2001-07-22 17:37:44 +03:00
|
|
|
/*
|
2003-06-07 21:34:02 +03:00
|
|
|
* Copyright (c) 2000-2003 Fabrice Bellard
|
2001-07-22 17:37:44 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2002-05-26 01:25:13 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2001-07-22 17:37:44 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2001-07-22 17:37:44 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-26 01:25:13 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 17:37:44 +03:00
|
|
|
*
|
2002-05-26 01:25:13 +03:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-07-22 17:37:44 +03:00
|
|
|
*/
|
2007-06-10 17:34:56 +03:00
|
|
|
|
2011-12-22 12:23:22 +03:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* multimedia converter based on the FFmpeg libraries
|
|
|
|
*/
|
|
|
|
|
2007-07-02 10:43:23 +03:00
|
|
|
#include "config.h"
|
2023-03-25 21:02:23 +02:00
|
|
|
|
2007-07-02 10:43:23 +03:00
|
|
|
#include <errno.h>
|
2004-05-12 19:51:39 +03:00
|
|
|
#include <limits.h>
|
2017-03-31 17:27:20 +02:00
|
|
|
#include <stdatomic.h>
|
2013-11-23 23:32:55 +03:00
|
|
|
#include <stdint.h>
|
2023-03-25 21:02:23 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2013-11-23 23:32:55 +03:00
|
|
|
|
2012-09-11 22:54:09 +03:00
|
|
|
#if HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_UNISTD_H
|
2008-08-15 01:01:59 +03:00
|
|
|
#include <unistd.h>
|
2012-06-22 23:34:02 +03:00
|
|
|
#endif
|
2013-11-24 07:21:19 +03:00
|
|
|
|
2009-01-14 01:44:16 +02:00
|
|
|
#if HAVE_SYS_RESOURCE_H
|
2012-10-14 02:27:26 +03:00
|
|
|
#include <sys/time.h>
|
2008-05-11 14:17:23 +03:00
|
|
|
#include <sys/types.h>
|
2008-05-08 03:44:42 +03:00
|
|
|
#include <sys/resource.h>
|
2009-01-14 01:44:16 +02:00
|
|
|
#elif HAVE_GETPROCESSTIMES
|
2007-07-13 19:11:36 +03:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2010-02-23 00:21:58 +02:00
|
|
|
#if HAVE_GETPROCESSMEMORYINFO
|
|
|
|
#include <windows.h>
|
|
|
|
#include <psapi.h>
|
|
|
|
#endif
|
2015-07-01 00:31:19 +02:00
|
|
|
#if HAVE_SETCONSOLECTRLHANDLER
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2009-01-14 01:44:16 +02:00
|
|
|
#if HAVE_SYS_SELECT_H
|
2008-09-26 05:13:38 +03:00
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
2011-03-30 21:58:13 +03:00
|
|
|
#if HAVE_TERMIOS_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#elif HAVE_KBHIT
|
2007-07-11 17:07:55 +03:00
|
|
|
#include <conio.h>
|
2001-08-14 00:43:02 +03:00
|
|
|
#endif
|
2012-06-10 22:34:15 +03:00
|
|
|
|
2023-03-25 21:02:23 +02:00
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/bprint.h"
|
|
|
|
#include "libavutil/channel_layout.h"
|
|
|
|
#include "libavutil/dict.h"
|
|
|
|
#include "libavutil/display.h"
|
|
|
|
#include "libavutil/fifo.h"
|
|
|
|
#include "libavutil/hwcontext.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/libm.h"
|
|
|
|
#include "libavutil/mathematics.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "libavutil/samplefmt.h"
|
|
|
|
#include "libavutil/thread.h"
|
|
|
|
#include "libavutil/threadmessage.h"
|
|
|
|
#include "libavutil/time.h"
|
|
|
|
#include "libavutil/timestamp.h"
|
|
|
|
|
|
|
|
#include "libavcodec/version.h"
|
|
|
|
|
|
|
|
#include "libavformat/avformat.h"
|
|
|
|
|
|
|
|
#include "libavdevice/avdevice.h"
|
|
|
|
|
|
|
|
#include "libswresample/swresample.h"
|
|
|
|
|
|
|
|
#include "libavfilter/avfilter.h"
|
|
|
|
#include "libavfilter/buffersrc.h"
|
|
|
|
#include "libavfilter/buffersink.h"
|
2001-07-22 17:37:44 +03:00
|
|
|
|
2003-06-07 21:34:02 +03:00
|
|
|
#include "cmdutils.h"
|
2023-03-25 21:02:23 +02:00
|
|
|
#include "ffmpeg.h"
|
2022-06-10 14:21:42 +02:00
|
|
|
#include "sync_queue.h"
|
2003-06-07 21:34:02 +03:00
|
|
|
|
2011-04-23 16:19:17 +03:00
|
|
|
const char program_name[] = "ffmpeg";
|
2008-05-29 11:48:51 +03:00
|
|
|
const int program_birth_year = 2000;
|
2007-09-27 09:38:40 +03:00
|
|
|
|
2023-03-25 12:49:54 +02:00
|
|
|
FILE *vstats_file;
|
2023-01-27 13:19:16 +02:00
|
|
|
|
2018-04-27 19:31:59 +02:00
|
|
|
typedef struct BenchmarkTimeStamps {
|
|
|
|
int64_t real_usec;
|
|
|
|
int64_t user_usec;
|
|
|
|
int64_t sys_usec;
|
|
|
|
} BenchmarkTimeStamps;
|
|
|
|
|
|
|
|
static BenchmarkTimeStamps get_benchmark_time_stamps(void);
|
2013-03-02 18:04:49 +03:00
|
|
|
static int64_t getmaxrss(void);
|
2003-01-23 00:40:52 +02:00
|
|
|
|
2021-11-10 15:51:48 +02:00
|
|
|
unsigned nb_output_dumped = 0;
|
2003-08-29 23:51:10 +03:00
|
|
|
|
2018-04-27 19:31:59 +02:00
|
|
|
static BenchmarkTimeStamps current_time;
|
2012-08-09 01:26:38 +03:00
|
|
|
AVIOContext *progress_avio = NULL;
|
2005-09-11 14:10:25 +03:00
|
|
|
|
2012-08-01 19:23:12 +03:00
|
|
|
InputFile **input_files = NULL;
|
|
|
|
int nb_input_files = 0;
|
2012-06-02 08:26:41 +03:00
|
|
|
|
2012-08-01 19:23:12 +03:00
|
|
|
OutputFile **output_files = NULL;
|
|
|
|
int nb_output_files = 0;
|
2001-07-22 17:37:44 +03:00
|
|
|
|
2012-08-01 19:23:12 +03:00
|
|
|
FilterGraph **filtergraphs;
|
|
|
|
int nb_filtergraphs;
|
2001-07-22 17:37:44 +03:00
|
|
|
|
2011-03-30 21:58:13 +03:00
|
|
|
#if HAVE_TERMIOS_H
|
|
|
|
|
|
|
|
/* init terminal so that we can grab keys */
|
|
|
|
static struct termios oldtty;
|
2012-01-17 02:10:17 +03:00
|
|
|
static int restore_tty;
|
2011-03-30 21:58:13 +03:00
|
|
|
#endif
|
|
|
|
|
2012-07-26 20:29:27 +03:00
|
|
|
/* sub2video hack:
|
|
|
|
Convert subtitles to video with alpha to insert them in filter graphs.
|
|
|
|
This is a temporary solution until libavfilter gets real subtitles support.
|
|
|
|
*/
|
|
|
|
|
2023-05-23 20:55:01 +02:00
|
|
|
static void sub2video_heartbeat(InputFile *infile, int64_t pts, AVRational tb)
|
2012-07-26 20:29:27 +03:00
|
|
|
{
|
|
|
|
/* When a frame is read from a file, examine all sub2video streams in
|
|
|
|
the same file and send the sub2video frame again. Otherwise, decoded
|
|
|
|
video frames could be accumulating in the filter graph while a filter
|
|
|
|
(possibly overlay) is desperately waiting for a subtitle frame. */
|
2023-05-24 10:08:33 +02:00
|
|
|
for (int i = 0; i < infile->nb_streams; i++) {
|
|
|
|
InputStream *ist = infile->streams[i];
|
|
|
|
|
|
|
|
if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE)
|
2012-07-26 20:29:27 +03:00
|
|
|
continue;
|
2023-05-24 10:08:33 +02:00
|
|
|
|
|
|
|
for (int j = 0; j < ist->nb_filters; j++)
|
|
|
|
ifilter_sub2video_heartbeat(ist->filters[j], pts, tb);
|
2012-07-26 20:29:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end of sub2video hack */
|
|
|
|
|
2014-03-20 17:39:45 +03:00
|
|
|
static void term_exit_sigsafe(void)
|
2011-09-05 03:04:27 +03:00
|
|
|
{
|
2012-08-09 01:26:38 +03:00
|
|
|
#if HAVE_TERMIOS_H
|
|
|
|
if(restore_tty)
|
|
|
|
tcsetattr (0, TCSANOW, &oldtty);
|
|
|
|
#endif
|
2011-07-27 21:56:59 +03:00
|
|
|
}
|
2011-09-05 03:04:27 +03:00
|
|
|
|
2014-03-20 17:39:45 +03:00
|
|
|
void term_exit(void)
|
|
|
|
{
|
|
|
|
av_log(NULL, AV_LOG_QUIET, "%s", "");
|
|
|
|
term_exit_sigsafe();
|
|
|
|
}
|
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
static volatile int received_sigterm = 0;
|
2012-08-09 05:12:50 +03:00
|
|
|
static volatile int received_nb_signals = 0;
|
2017-03-31 17:27:20 +02:00
|
|
|
static atomic_int transcode_init_done = ATOMIC_VAR_INIT(0);
|
2015-07-01 00:31:19 +02:00
|
|
|
static volatile int ffmpeg_exited = 0;
|
2020-10-20 16:51:08 +02:00
|
|
|
static int64_t copy_ts_first_pts = AV_NOPTS_VALUE;
|
2011-09-05 03:04:27 +03:00
|
|
|
|
2012-08-09 05:12:50 +03:00
|
|
|
static void
|
|
|
|
sigterm_handler(int sig)
|
2011-07-27 21:56:59 +03:00
|
|
|
{
|
2017-11-18 07:24:24 +02:00
|
|
|
int ret;
|
2011-07-27 21:56:59 +03:00
|
|
|
received_sigterm = sig;
|
|
|
|
received_nb_signals++;
|
2014-03-20 17:39:45 +03:00
|
|
|
term_exit_sigsafe();
|
2015-06-25 12:22:10 +02:00
|
|
|
if(received_nb_signals > 3) {
|
2017-11-18 07:24:24 +02:00
|
|
|
ret = write(2/*STDERR_FILENO*/, "Received > 3 system signals, hard exiting\n",
|
|
|
|
strlen("Received > 3 system signals, hard exiting\n"));
|
|
|
|
if (ret < 0) { /* Do nothing */ };
|
2014-03-21 17:45:03 +03:00
|
|
|
exit(123);
|
2015-06-25 12:22:10 +02:00
|
|
|
}
|
2011-09-05 03:04:27 +03:00
|
|
|
}
|
|
|
|
|
2015-07-01 00:31:19 +02:00
|
|
|
#if HAVE_SETCONSOLECTRLHANDLER
|
|
|
|
static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
|
|
|
|
{
|
|
|
|
av_log(NULL, AV_LOG_DEBUG, "\nReceived windows signal %ld\n", fdwCtrlType);
|
|
|
|
|
|
|
|
switch (fdwCtrlType)
|
|
|
|
{
|
|
|
|
case CTRL_C_EVENT:
|
|
|
|
case CTRL_BREAK_EVENT:
|
|
|
|
sigterm_handler(SIGINT);
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case CTRL_CLOSE_EVENT:
|
|
|
|
case CTRL_LOGOFF_EVENT:
|
|
|
|
case CTRL_SHUTDOWN_EVENT:
|
|
|
|
sigterm_handler(SIGTERM);
|
|
|
|
/* Basically, with these 3 events, when we return from this method the
|
|
|
|
process is hard terminated, so stall as long as we need to
|
|
|
|
to try and let the main thread(s) clean up and gracefully terminate
|
|
|
|
(we have at most 5 seconds, but should be done far before that). */
|
|
|
|
while (!ffmpeg_exited) {
|
|
|
|
Sleep(0);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Received unknown windows signal %ld\n", fdwCtrlType);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-11-28 21:46:53 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
#define SIGNAL(sig, func) \
|
|
|
|
do { \
|
|
|
|
action.sa_handler = func; \
|
|
|
|
sigaction(sig, &action, NULL); \
|
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define SIGNAL(sig, func) \
|
|
|
|
signal(sig, func)
|
|
|
|
#endif
|
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
void term_init(void)
|
2010-05-07 12:43:21 +03:00
|
|
|
{
|
2020-11-28 21:46:53 +02:00
|
|
|
#if defined __linux__
|
|
|
|
struct sigaction action = {0};
|
|
|
|
action.sa_handler = sigterm_handler;
|
|
|
|
|
|
|
|
/* block other interrupts while processing this one */
|
|
|
|
sigfillset(&action.sa_mask);
|
|
|
|
|
|
|
|
/* restart interruptible functions (i.e. don't fail with EINTR) */
|
|
|
|
action.sa_flags = SA_RESTART;
|
|
|
|
#endif
|
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
#if HAVE_TERMIOS_H
|
2022-04-03 17:24:40 +02:00
|
|
|
if (stdin_interaction) {
|
2012-08-09 01:26:38 +03:00
|
|
|
struct termios tty;
|
2015-07-31 14:36:27 +02:00
|
|
|
if (tcgetattr (0, &tty) == 0) {
|
2012-08-09 01:26:38 +03:00
|
|
|
oldtty = tty;
|
|
|
|
restore_tty = 1;
|
|
|
|
|
|
|
|
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
|
|
|
|
|INLCR|IGNCR|ICRNL|IXON);
|
|
|
|
tty.c_oflag |= OPOST;
|
|
|
|
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
|
|
|
|
tty.c_cflag &= ~(CSIZE|PARENB);
|
|
|
|
tty.c_cflag |= CS8;
|
|
|
|
tty.c_cc[VMIN] = 1;
|
|
|
|
tty.c_cc[VTIME] = 0;
|
|
|
|
|
|
|
|
tcsetattr (0, TCSANOW, &tty);
|
2012-04-17 05:01:17 +03:00
|
|
|
}
|
2020-11-28 21:46:53 +02:00
|
|
|
SIGNAL(SIGQUIT, sigterm_handler); /* Quit (POSIX). */
|
2012-04-17 05:01:17 +03:00
|
|
|
}
|
2012-08-09 01:26:38 +03:00
|
|
|
#endif
|
|
|
|
|
2020-11-28 21:46:53 +02:00
|
|
|
SIGNAL(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
|
|
|
|
SIGNAL(SIGTERM, sigterm_handler); /* Termination (ANSI). */
|
2011-07-27 21:56:59 +03:00
|
|
|
#ifdef SIGXCPU
|
2020-11-28 21:46:53 +02:00
|
|
|
SIGNAL(SIGXCPU, sigterm_handler);
|
2011-07-27 21:56:59 +03:00
|
|
|
#endif
|
2018-01-19 01:24:05 +02:00
|
|
|
#ifdef SIGPIPE
|
|
|
|
signal(SIGPIPE, SIG_IGN); /* Broken pipe (POSIX). */
|
|
|
|
#endif
|
2015-07-01 00:31:19 +02:00
|
|
|
#if HAVE_SETCONSOLECTRLHANDLER
|
|
|
|
SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE);
|
|
|
|
#endif
|
2012-04-17 05:01:17 +03:00
|
|
|
}
|
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
/* read a key without blocking */
|
|
|
|
static int read_key(void)
|
2012-03-23 17:46:30 +03:00
|
|
|
{
|
2012-08-09 01:26:38 +03:00
|
|
|
unsigned char ch;
|
|
|
|
#if HAVE_TERMIOS_H
|
|
|
|
int n = 1;
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set rfds;
|
2012-04-27 07:56:56 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(0, &rfds);
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
n = select(1, &rfds, NULL, NULL, &tv);
|
|
|
|
if (n > 0) {
|
|
|
|
n = read(0, &ch, 1);
|
|
|
|
if (n == 1)
|
|
|
|
return ch;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
#elif HAVE_KBHIT
|
|
|
|
# if HAVE_PEEKNAMEDPIPE
|
|
|
|
static int is_pipe;
|
|
|
|
static HANDLE input_handle;
|
|
|
|
DWORD dw, nchars;
|
|
|
|
if(!input_handle){
|
|
|
|
input_handle = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
is_pipe = !GetConsoleMode(input_handle, &dw);
|
|
|
|
}
|
2012-05-10 03:14:44 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
if (is_pipe) {
|
|
|
|
/* When running under a GUI, you will end here. */
|
2012-12-26 22:21:15 +03:00
|
|
|
if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL)) {
|
|
|
|
// input pipe may have been closed by the program that ran ffmpeg
|
2012-08-09 01:26:38 +03:00
|
|
|
return -1;
|
2012-12-26 22:21:15 +03:00
|
|
|
}
|
2012-08-09 01:26:38 +03:00
|
|
|
//Read it
|
|
|
|
if(nchars != 0) {
|
|
|
|
read(0, &ch, 1);
|
|
|
|
return ch;
|
|
|
|
}else{
|
|
|
|
return -1;
|
2012-05-05 19:22:46 +03:00
|
|
|
}
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
|
|
|
# endif
|
|
|
|
if(kbhit())
|
|
|
|
return(getch());
|
|
|
|
#endif
|
|
|
|
return -1;
|
2012-03-23 17:46:30 +03:00
|
|
|
}
|
|
|
|
|
2011-11-07 00:22:04 +03:00
|
|
|
static int decode_interrupt_cb(void *ctx)
|
2011-07-27 21:56:59 +03:00
|
|
|
{
|
2017-03-31 17:27:20 +02:00
|
|
|
return received_nb_signals > atomic_load(&transcode_init_done);
|
2012-05-05 19:22:46 +03:00
|
|
|
}
|
|
|
|
|
2012-08-01 19:23:12 +03:00
|
|
|
const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
|
2012-05-05 19:22:46 +03:00
|
|
|
|
2013-07-08 05:34:11 +03:00
|
|
|
static void ffmpeg_cleanup(int ret)
|
2011-07-27 21:56:59 +03:00
|
|
|
{
|
2023-04-18 19:41:48 +02:00
|
|
|
int i;
|
2012-05-05 19:22:46 +03:00
|
|
|
|
2013-03-02 18:04:49 +03:00
|
|
|
if (do_benchmark) {
|
|
|
|
int maxrss = getmaxrss() / 1024;
|
2015-07-09 19:58:22 +02:00
|
|
|
av_log(NULL, AV_LOG_INFO, "bench: maxrss=%ikB\n", maxrss);
|
2013-03-02 18:04:49 +03:00
|
|
|
}
|
|
|
|
|
2023-04-18 19:41:48 +02:00
|
|
|
for (i = 0; i < nb_filtergraphs; i++)
|
|
|
|
fg_free(&filtergraphs[i]);
|
2012-03-29 09:51:17 +03:00
|
|
|
av_freep(&filtergraphs);
|
2012-05-05 19:22:46 +03:00
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
/* close files */
|
2022-10-14 11:18:00 +02:00
|
|
|
for (i = 0; i < nb_output_files; i++)
|
2022-10-14 10:57:19 +02:00
|
|
|
of_close(&output_files[i]);
|
2016-09-21 15:39:16 +02:00
|
|
|
|
2022-10-17 11:37:45 +02:00
|
|
|
for (i = 0; i < nb_input_files; i++)
|
|
|
|
ifile_close(&input_files[i]);
|
|
|
|
|
2016-01-07 06:57:47 +02:00
|
|
|
if (vstats_file) {
|
|
|
|
if (fclose(vstats_file))
|
|
|
|
av_log(NULL, AV_LOG_ERROR,
|
|
|
|
"Error closing vstats file, loss of information possible: %s\n",
|
|
|
|
av_err2str(AVERROR(errno)));
|
|
|
|
}
|
2014-12-28 03:56:02 +02:00
|
|
|
av_freep(&vstats_filename);
|
2022-11-25 15:00:48 +02:00
|
|
|
of_enc_stats_close();
|
|
|
|
|
2023-03-28 09:42:30 +02:00
|
|
|
hw_device_free_all();
|
|
|
|
|
2021-09-03 15:30:32 +02:00
|
|
|
av_freep(&filter_nbthreads);
|
2012-03-29 09:51:17 +03:00
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
av_freep(&input_files);
|
2011-08-17 10:56:08 +03:00
|
|
|
av_freep(&output_files);
|
2012-05-26 13:47:47 +03:00
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
uninit_opts();
|
2012-03-29 09:51:17 +03:00
|
|
|
|
2011-11-06 03:47:48 +03:00
|
|
|
avformat_network_deinit();
|
2012-03-29 09:51:17 +03:00
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
if (received_sigterm) {
|
2015-06-25 08:24:31 +02:00
|
|
|
av_log(NULL, AV_LOG_INFO, "Exiting normally, received signal %d.\n",
|
2011-09-13 08:31:21 +03:00
|
|
|
(int) received_sigterm);
|
2017-03-31 17:27:20 +02:00
|
|
|
} else if (ret && atomic_load(&transcode_init_done)) {
|
2014-03-31 05:31:28 +03:00
|
|
|
av_log(NULL, AV_LOG_INFO, "Conversion failed!\n");
|
2011-07-27 21:56:59 +03:00
|
|
|
}
|
2013-07-08 05:34:11 +03:00
|
|
|
term_exit();
|
2015-07-01 00:31:19 +02:00
|
|
|
ffmpeg_exited = 1;
|
2012-03-29 09:51:17 +03:00
|
|
|
}
|
|
|
|
|
2023-03-25 12:49:54 +02:00
|
|
|
OutputStream *ost_iter(OutputStream *prev)
|
2022-10-14 10:57:19 +02:00
|
|
|
{
|
|
|
|
int of_idx = prev ? prev->file_index : 0;
|
|
|
|
int ost_idx = prev ? prev->index + 1 : 0;
|
|
|
|
|
|
|
|
for (; of_idx < nb_output_files; of_idx++) {
|
|
|
|
OutputFile *of = output_files[of_idx];
|
2022-10-19 13:46:14 +02:00
|
|
|
if (ost_idx < of->nb_streams)
|
2022-10-14 10:57:19 +02:00
|
|
|
return of->streams[ost_idx];
|
|
|
|
|
|
|
|
ost_idx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-17 10:49:27 +02:00
|
|
|
InputStream *ist_iter(InputStream *prev)
|
|
|
|
{
|
|
|
|
int if_idx = prev ? prev->file_index : 0;
|
2023-05-19 09:53:57 +02:00
|
|
|
int ist_idx = prev ? prev->index + 1 : 0;
|
2022-11-17 10:49:27 +02:00
|
|
|
|
|
|
|
for (; if_idx < nb_input_files; if_idx++) {
|
|
|
|
InputFile *f = input_files[if_idx];
|
|
|
|
if (ist_idx < f->nb_streams)
|
|
|
|
return f->streams[ist_idx];
|
|
|
|
|
|
|
|
ist_idx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-05-27 12:25:51 +02:00
|
|
|
FrameData *frame_data(AVFrame *frame)
|
|
|
|
{
|
|
|
|
if (!frame->opaque_ref) {
|
2023-07-07 11:02:15 +02:00
|
|
|
FrameData *fd;
|
|
|
|
|
|
|
|
frame->opaque_ref = av_buffer_allocz(sizeof(*fd));
|
2023-05-27 12:25:51 +02:00
|
|
|
if (!frame->opaque_ref)
|
|
|
|
return NULL;
|
2023-07-07 11:02:15 +02:00
|
|
|
fd = (FrameData*)frame->opaque_ref->data;
|
|
|
|
|
|
|
|
fd->dec.frame_num = UINT64_MAX;
|
|
|
|
fd->dec.pts = AV_NOPTS_VALUE;
|
2023-05-27 12:25:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (FrameData*)frame->opaque_ref->data;
|
|
|
|
}
|
|
|
|
|
2014-08-04 14:32:11 +03:00
|
|
|
void remove_avoptions(AVDictionary **a, AVDictionary *b)
|
|
|
|
{
|
2021-10-19 00:27:01 +02:00
|
|
|
const AVDictionaryEntry *t = NULL;
|
2014-08-04 14:32:11 +03:00
|
|
|
|
2022-11-26 16:46:18 +02:00
|
|
|
while ((t = av_dict_iterate(b, t))) {
|
2014-08-04 14:32:11 +03:00
|
|
|
av_dict_set(a, t->key, NULL, AV_DICT_MATCH_CASE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:18:57 +02:00
|
|
|
int check_avoptions(AVDictionary *m)
|
2012-04-01 16:08:33 +03:00
|
|
|
{
|
2021-10-19 00:27:01 +02:00
|
|
|
const AVDictionaryEntry *t;
|
2011-07-27 21:56:59 +03:00
|
|
|
if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
|
2011-09-12 22:51:02 +03:00
|
|
|
av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
|
2023-07-14 18:18:57 +02:00
|
|
|
return AVERROR_OPTION_NOT_FOUND;
|
2012-04-01 16:08:33 +03:00
|
|
|
}
|
2023-07-14 18:18:57 +02:00
|
|
|
|
|
|
|
return 0;
|
2011-07-27 21:56:59 +03:00
|
|
|
}
|
2012-04-01 16:08:33 +03:00
|
|
|
|
2023-03-25 12:49:54 +02:00
|
|
|
void update_benchmark(const char *fmt, ...)
|
2011-07-27 21:56:59 +03:00
|
|
|
{
|
2012-08-09 01:26:38 +03:00
|
|
|
if (do_benchmark_all) {
|
2018-04-27 19:31:59 +02:00
|
|
|
BenchmarkTimeStamps t = get_benchmark_time_stamps();
|
2012-08-09 01:26:38 +03:00
|
|
|
va_list va;
|
|
|
|
char buf[1024];
|
2012-04-01 16:08:33 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
if (fmt) {
|
|
|
|
va_start(va, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, va);
|
|
|
|
va_end(va);
|
2018-04-27 19:31:59 +02:00
|
|
|
av_log(NULL, AV_LOG_INFO,
|
|
|
|
"bench: %8" PRIu64 " user %8" PRIu64 " sys %8" PRIu64 " real %s \n",
|
|
|
|
t.user_usec - current_time.user_usec,
|
|
|
|
t.sys_usec - current_time.sys_usec,
|
|
|
|
t.real_usec - current_time.real_usec, buf);
|
2012-04-01 16:08:33 +03:00
|
|
|
}
|
2012-08-09 01:26:38 +03:00
|
|
|
current_time = t;
|
2012-04-01 16:08:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 12:49:54 +02:00
|
|
|
void close_output_stream(OutputStream *ost)
|
2012-08-20 11:57:18 +03:00
|
|
|
{
|
|
|
|
OutputFile *of = output_files[ost->file_index];
|
2014-01-18 01:52:55 +03:00
|
|
|
ost->finished |= ENCODER_FINISHED;
|
2022-06-10 14:21:42 +02:00
|
|
|
|
|
|
|
if (ost->sq_idx_encode >= 0)
|
|
|
|
sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
|
2012-08-20 11:57:18 +03:00
|
|
|
}
|
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)
|
2011-09-12 12:44:14 +03:00
|
|
|
{
|
2018-02-26 16:13:29 +02:00
|
|
|
AVBPrint buf, buf_script;
|
2021-12-11 15:44:38 +02:00
|
|
|
int64_t total_size = of_filesize(output_files[0]);
|
2022-10-14 10:57:19 +02:00
|
|
|
int vid;
|
2012-08-09 01:26:38 +03:00
|
|
|
double bitrate;
|
2015-12-11 17:49:14 +02:00
|
|
|
double speed;
|
2023-07-03 23:17:01 +02:00
|
|
|
int64_t pts = AV_NOPTS_VALUE;
|
2011-07-27 21:56:59 +03:00
|
|
|
static int64_t last_time = -1;
|
2020-12-21 11:16:15 +02:00
|
|
|
static int first_report = 1;
|
2023-05-28 16:51:57 +02:00
|
|
|
uint64_t nb_frames_dup = 0, nb_frames_drop = 0;
|
2023-07-03 23:17:01 +02:00
|
|
|
int mins, secs, us;
|
|
|
|
int64_t hours;
|
2018-02-12 12:11:23 +02:00
|
|
|
const char *hours_sign;
|
2016-01-05 05:33:50 +02:00
|
|
|
int ret;
|
2015-12-11 17:49:14 +02:00
|
|
|
float t;
|
2012-07-03 21:34:27 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
if (!print_stats && !is_last_report && !progress_avio)
|
2012-05-05 19:22:46 +03:00
|
|
|
return;
|
2012-07-03 21:34:27 +03:00
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
if (!is_last_report) {
|
|
|
|
if (last_time == -1) {
|
|
|
|
last_time = cur_time;
|
2009-05-31 23:19:16 +03:00
|
|
|
}
|
2020-12-23 06:53:30 +02:00
|
|
|
if (((cur_time - last_time) < stats_period && !first_report) ||
|
|
|
|
(first_report && nb_output_dumped < nb_output_files))
|
2011-07-27 21:56:59 +03:00
|
|
|
return;
|
|
|
|
last_time = cur_time;
|
2002-11-20 05:06:12 +02:00
|
|
|
}
|
|
|
|
|
2015-12-11 17:49:14 +02:00
|
|
|
t = (cur_time-timer_start) / 1000000.0;
|
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
vid = 0;
|
2018-02-26 16:13:29 +02:00
|
|
|
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
|
2018-06-10 09:56:04 +02:00
|
|
|
av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC);
|
2022-10-14 10:57:19 +02:00
|
|
|
for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
|
2023-04-20 09:06:25 +02:00
|
|
|
const float q = ost->enc ? ost->quality / (float) FF_QP2LAMBDA : -1;
|
2015-07-21 00:54:42 +02:00
|
|
|
|
2023-04-11 10:15:55 +02:00
|
|
|
if (vid && ost->type == AVMEDIA_TYPE_VIDEO) {
|
2018-02-26 16:13:29 +02:00
|
|
|
av_bprintf(&buf, "q=%2.1f ", q);
|
2012-08-09 01:26:38 +03:00
|
|
|
av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
|
|
|
|
ost->file_index, ost->index, q);
|
2005-06-03 17:31:45 +03:00
|
|
|
}
|
2023-04-11 10:15:55 +02:00
|
|
|
if (!vid && ost->type == AVMEDIA_TYPE_VIDEO) {
|
2015-12-11 17:49:14 +02:00
|
|
|
float fps;
|
2022-03-31 17:33:48 +02:00
|
|
|
uint64_t frame_number = atomic_load(&ost->packets_written);
|
2005-06-03 17:31:45 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
fps = t > 1 ? frame_number / t : 0;
|
2022-06-20 20:10:21 +02:00
|
|
|
av_bprintf(&buf, "frame=%5"PRId64" fps=%3.*f q=%3.1f ",
|
2012-08-09 01:26:38 +03:00
|
|
|
frame_number, fps < 9.95, fps, q);
|
2022-06-20 20:10:21 +02:00
|
|
|
av_bprintf(&buf_script, "frame=%"PRId64"\n", frame_number);
|
2018-04-28 01:00:33 +02:00
|
|
|
av_bprintf(&buf_script, "fps=%.2f\n", fps);
|
2012-08-09 01:26:38 +03:00
|
|
|
av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
|
|
|
|
ost->file_index, ost->index, q);
|
2011-12-29 23:28:29 +03:00
|
|
|
if (is_last_report)
|
2018-02-26 16:13:29 +02:00
|
|
|
av_bprintf(&buf, "L");
|
2015-07-21 01:12:59 +02:00
|
|
|
|
2023-05-28 16:51:57 +02:00
|
|
|
nb_frames_dup = ost->nb_frames_dup;
|
|
|
|
nb_frames_drop = ost->nb_frames_drop;
|
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
vid = 1;
|
2001-07-22 17:37:44 +03:00
|
|
|
}
|
2011-07-27 21:56:59 +03:00
|
|
|
/* compute min output value */
|
2022-04-01 12:04:35 +02:00
|
|
|
if (ost->last_mux_dts != AV_NOPTS_VALUE) {
|
2023-07-03 23:17:01 +02:00
|
|
|
if (pts == AV_NOPTS_VALUE || ost->last_mux_dts > pts)
|
|
|
|
pts = ost->last_mux_dts;
|
2020-10-20 16:51:08 +02:00
|
|
|
if (copy_ts) {
|
|
|
|
if (copy_ts_first_pts == AV_NOPTS_VALUE && pts > 1)
|
|
|
|
copy_ts_first_pts = pts;
|
|
|
|
if (copy_ts_first_pts != AV_NOPTS_VALUE)
|
|
|
|
pts -= copy_ts_first_pts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-19 04:10:12 +02:00
|
|
|
if (is_last_report)
|
2015-11-15 04:23:20 +02:00
|
|
|
nb_frames_drop += ost->last_dropped;
|
2001-07-22 17:37:44 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 23:17:01 +02:00
|
|
|
us = FFABS64U(pts) % AV_TIME_BASE;
|
|
|
|
secs = FFABS64U(pts) / AV_TIME_BASE % 60;
|
|
|
|
mins = FFABS64U(pts) / AV_TIME_BASE / 60 % 60;
|
|
|
|
hours = FFABS64U(pts) / AV_TIME_BASE / 3600;
|
2018-02-12 12:11:23 +02:00
|
|
|
hours_sign = (pts < 0) ? "-" : "";
|
2010-02-03 00:23:09 +02:00
|
|
|
|
2023-07-03 23:17:01 +02:00
|
|
|
bitrate = pts != AV_NOPTS_VALUE && pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
|
|
|
|
speed = pts != AV_NOPTS_VALUE && t != 0.0 ? (double)pts / AV_TIME_BASE / t : -1;
|
2011-09-12 00:35:35 +03:00
|
|
|
|
2018-02-26 16:13:29 +02:00
|
|
|
if (total_size < 0) av_bprintf(&buf, "size=N/A time=");
|
|
|
|
else av_bprintf(&buf, "size=%8.0fkB time=", total_size / 1024.0);
|
2018-02-12 12:11:23 +02:00
|
|
|
if (pts == AV_NOPTS_VALUE) {
|
2018-02-26 16:13:29 +02:00
|
|
|
av_bprintf(&buf, "N/A ");
|
2018-02-12 12:11:23 +02:00
|
|
|
} else {
|
2023-07-03 23:17:01 +02:00
|
|
|
av_bprintf(&buf, "%s%02"PRId64":%02d:%02d.%02d ",
|
2018-02-26 16:13:29 +02:00
|
|
|
hours_sign, hours, mins, secs, (100 * us) / AV_TIME_BASE);
|
2018-02-12 12:11:23 +02:00
|
|
|
}
|
2015-01-08 04:50:15 +02:00
|
|
|
|
|
|
|
if (bitrate < 0) {
|
2018-02-26 16:13:29 +02:00
|
|
|
av_bprintf(&buf, "bitrate=N/A");
|
2015-01-08 04:50:15 +02:00
|
|
|
av_bprintf(&buf_script, "bitrate=N/A\n");
|
|
|
|
}else{
|
2018-02-26 16:13:29 +02:00
|
|
|
av_bprintf(&buf, "bitrate=%6.1fkbits/s", bitrate);
|
2015-01-08 04:50:15 +02:00
|
|
|
av_bprintf(&buf_script, "bitrate=%6.1fkbits/s\n", bitrate);
|
|
|
|
}
|
|
|
|
|
2012-12-23 02:19:11 +03:00
|
|
|
if (total_size < 0) av_bprintf(&buf_script, "total_size=N/A\n");
|
|
|
|
else av_bprintf(&buf_script, "total_size=%"PRId64"\n", total_size);
|
2018-02-12 12:11:23 +02:00
|
|
|
if (pts == AV_NOPTS_VALUE) {
|
2018-08-05 09:04:21 +02:00
|
|
|
av_bprintf(&buf_script, "out_time_us=N/A\n");
|
2018-02-12 12:11:23 +02:00
|
|
|
av_bprintf(&buf_script, "out_time_ms=N/A\n");
|
|
|
|
av_bprintf(&buf_script, "out_time=N/A\n");
|
|
|
|
} else {
|
2018-08-05 09:04:21 +02:00
|
|
|
av_bprintf(&buf_script, "out_time_us=%"PRId64"\n", pts);
|
2018-02-12 12:11:23 +02:00
|
|
|
av_bprintf(&buf_script, "out_time_ms=%"PRId64"\n", pts);
|
2023-07-03 23:17:01 +02:00
|
|
|
av_bprintf(&buf_script, "out_time=%s%02"PRId64":%02d:%02d.%06d\n",
|
2018-02-12 12:11:23 +02:00
|
|
|
hours_sign, hours, mins, secs, us);
|
|
|
|
}
|
2011-07-27 21:56:59 +03:00
|
|
|
|
2011-09-13 08:31:21 +03:00
|
|
|
if (nb_frames_dup || nb_frames_drop)
|
2022-06-20 20:10:21 +02:00
|
|
|
av_bprintf(&buf, " dup=%"PRId64" drop=%"PRId64, nb_frames_dup, nb_frames_drop);
|
|
|
|
av_bprintf(&buf_script, "dup_frames=%"PRId64"\n", nb_frames_dup);
|
|
|
|
av_bprintf(&buf_script, "drop_frames=%"PRId64"\n", nb_frames_drop);
|
2011-07-27 21:56:59 +03:00
|
|
|
|
2015-12-11 17:49:14 +02:00
|
|
|
if (speed < 0) {
|
2018-02-26 16:13:29 +02:00
|
|
|
av_bprintf(&buf, " speed=N/A");
|
2015-12-11 17:49:14 +02:00
|
|
|
av_bprintf(&buf_script, "speed=N/A\n");
|
|
|
|
} else {
|
2018-02-26 16:13:29 +02:00
|
|
|
av_bprintf(&buf, " speed=%4.3gx", speed);
|
2015-12-11 17:49:14 +02:00
|
|
|
av_bprintf(&buf_script, "speed=%4.3gx\n", speed);
|
|
|
|
}
|
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
if (print_stats || is_last_report) {
|
2014-11-19 04:05:09 +02:00
|
|
|
const char end = is_last_report ? '\n' : '\r';
|
2013-03-03 21:33:37 +03:00
|
|
|
if (print_stats==1 && AV_LOG_INFO > av_log_get_level()) {
|
2018-02-26 16:13:29 +02:00
|
|
|
fprintf(stderr, "%s %c", buf.str, end);
|
2013-03-03 21:33:37 +03:00
|
|
|
} else
|
2018-02-26 16:13:29 +02:00
|
|
|
av_log(NULL, AV_LOG_INFO, "%s %c", buf.str, end);
|
2011-07-27 21:56:59 +03:00
|
|
|
|
2018-11-09 14:33:51 +02:00
|
|
|
fflush(stderr);
|
2001-07-22 17:37:44 +03:00
|
|
|
}
|
2018-02-26 16:13:29 +02:00
|
|
|
av_bprint_finalize(&buf, NULL);
|
2001-07-22 17:37:44 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
if (progress_avio) {
|
|
|
|
av_bprintf(&buf_script, "progress=%s\n",
|
|
|
|
is_last_report ? "end" : "continue");
|
|
|
|
avio_write(progress_avio, buf_script.str,
|
|
|
|
FFMIN(buf_script.len, buf_script.size - 1));
|
|
|
|
avio_flush(progress_avio);
|
|
|
|
av_bprint_finalize(&buf_script, NULL);
|
|
|
|
if (is_last_report) {
|
2016-01-05 05:33:50 +02:00
|
|
|
if ((ret = avio_closep(&progress_avio)) < 0)
|
|
|
|
av_log(NULL, AV_LOG_ERROR,
|
|
|
|
"Error closing progress log, loss of information possible: %s\n", av_err2str(ret));
|
2007-08-20 11:12:08 +03:00
|
|
|
}
|
|
|
|
}
|
2009-03-11 08:06:18 +02:00
|
|
|
|
2020-12-21 11:16:15 +02:00
|
|
|
first_report = 0;
|
2011-07-27 21:56:59 +03:00
|
|
|
}
|
2009-03-11 08:06:18 +02:00
|
|
|
|
2023-05-23 13:00:12 +02:00
|
|
|
int copy_av_subtitle(AVSubtitle *dst, const AVSubtitle *src)
|
2022-07-22 12:57:54 +02:00
|
|
|
{
|
|
|
|
int ret = AVERROR_BUG;
|
|
|
|
AVSubtitle tmp = {
|
|
|
|
.format = src->format,
|
|
|
|
.start_display_time = src->start_display_time,
|
|
|
|
.end_display_time = src->end_display_time,
|
|
|
|
.num_rects = 0,
|
|
|
|
.rects = NULL,
|
|
|
|
.pts = src->pts
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!src->num_rects)
|
|
|
|
goto success;
|
|
|
|
|
|
|
|
if (!(tmp.rects = av_calloc(src->num_rects, sizeof(*tmp.rects))))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (int i = 0; i < src->num_rects; i++) {
|
|
|
|
AVSubtitleRect *src_rect = src->rects[i];
|
|
|
|
AVSubtitleRect *dst_rect;
|
|
|
|
|
|
|
|
if (!(dst_rect = tmp.rects[i] = av_mallocz(sizeof(*tmp.rects[0])))) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp.num_rects++;
|
|
|
|
|
|
|
|
dst_rect->type = src_rect->type;
|
|
|
|
dst_rect->flags = src_rect->flags;
|
|
|
|
|
|
|
|
dst_rect->x = src_rect->x;
|
|
|
|
dst_rect->y = src_rect->y;
|
|
|
|
dst_rect->w = src_rect->w;
|
|
|
|
dst_rect->h = src_rect->h;
|
|
|
|
dst_rect->nb_colors = src_rect->nb_colors;
|
|
|
|
|
|
|
|
if (src_rect->text)
|
|
|
|
if (!(dst_rect->text = av_strdup(src_rect->text))) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src_rect->ass)
|
|
|
|
if (!(dst_rect->ass = av_strdup(src_rect->ass))) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < 4; j++) {
|
|
|
|
// SUBTITLE_BITMAP images are special in the sense that they
|
|
|
|
// are like PAL8 images. first pointer to data, second to
|
|
|
|
// palette. This makes the size calculation match this.
|
|
|
|
size_t buf_size = src_rect->type == SUBTITLE_BITMAP && j == 1 ?
|
|
|
|
AVPALETTE_SIZE :
|
|
|
|
src_rect->h * src_rect->linesize[j];
|
|
|
|
|
|
|
|
if (!src_rect->data[j])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!(dst_rect->data[j] = av_memdup(src_rect->data[j], buf_size))) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
dst_rect->linesize[j] = src_rect->linesize[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
success:
|
|
|
|
*dst = tmp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
avsubtitle_free(&tmp);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-06-02 09:59:31 +02:00
|
|
|
static void subtitle_free(void *opaque, uint8_t *data)
|
|
|
|
{
|
|
|
|
AVSubtitle *sub = (AVSubtitle*)data;
|
|
|
|
avsubtitle_free(sub);
|
|
|
|
av_free(sub);
|
|
|
|
}
|
|
|
|
|
|
|
|
int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy)
|
|
|
|
{
|
|
|
|
AVBufferRef *buf;
|
|
|
|
AVSubtitle *sub;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (copy) {
|
|
|
|
sub = av_mallocz(sizeof(*sub));
|
|
|
|
ret = sub ? copy_av_subtitle(sub, subtitle) : AVERROR(ENOMEM);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_freep(&sub);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sub = av_memdup(subtitle, sizeof(*subtitle));
|
|
|
|
if (!sub)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
memset(subtitle, 0, sizeof(*subtitle));
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = av_buffer_create((uint8_t*)sub, sizeof(*sub),
|
|
|
|
subtitle_free, NULL, 0);
|
|
|
|
if (!buf) {
|
|
|
|
avsubtitle_free(sub);
|
|
|
|
av_freep(&sub);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
frame->buf[0] = buf;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-25 12:49:54 +02:00
|
|
|
int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt)
|
2022-07-22 12:57:54 +02:00
|
|
|
{
|
|
|
|
OutputFile *of = output_files[ost->file_index];
|
2023-02-06 14:12:05 +02:00
|
|
|
int64_t signal_pts = av_rescale_q(pkt->pts, pkt->time_base,
|
2022-07-22 12:57:54 +02:00
|
|
|
AV_TIME_BASE_Q);
|
|
|
|
|
|
|
|
if (!ost->fix_sub_duration_heartbeat || !(pkt->flags & AV_PKT_FLAG_KEY))
|
|
|
|
// we are only interested in heartbeats on streams configured, and
|
|
|
|
// only on random access points.
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < of->nb_streams; i++) {
|
|
|
|
OutputStream *iter_ost = of->streams[i];
|
|
|
|
InputStream *ist = iter_ost->ist;
|
|
|
|
int ret = AVERROR_BUG;
|
|
|
|
|
|
|
|
if (iter_ost == ost || !ist || !ist->decoding_needed ||
|
|
|
|
ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE)
|
|
|
|
// We wish to skip the stream that causes the heartbeat,
|
|
|
|
// output streams without an input stream, streams not decoded
|
|
|
|
// (as fix_sub_duration is only done for decoded subtitles) as
|
|
|
|
// well as non-subtitle streams.
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((ret = fix_sub_duration_heartbeat(ist, signal_pts)) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-17 09:29:30 +02:00
|
|
|
/* pkt = NULL means EOF (needed to flush decoder buffers) */
|
|
|
|
static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
|
|
|
|
{
|
|
|
|
InputFile *f = input_files[ist->file_index];
|
|
|
|
int64_t dts_est = AV_NOPTS_VALUE;
|
|
|
|
int ret = 0;
|
|
|
|
int eof_reached = 0;
|
2001-07-22 17:37:44 +03:00
|
|
|
|
2023-07-13 15:11:07 +02:00
|
|
|
if (ist->decoding_needed) {
|
2023-05-17 09:29:30 +02:00
|
|
|
ret = dec_packet(ist, pkt, no_eof);
|
2023-07-13 15:11:07 +02:00
|
|
|
if (ret < 0 && ret != AVERROR_EOF)
|
|
|
|
return ret;
|
|
|
|
}
|
2023-05-17 09:29:30 +02:00
|
|
|
if (ret == AVERROR_EOF || (!pkt && !ist->decoding_needed))
|
2022-02-14 19:07:01 +02:00
|
|
|
eof_reached = 1;
|
|
|
|
|
2023-04-27 13:54:36 +02:00
|
|
|
if (pkt && pkt->opaque_ref) {
|
|
|
|
DemuxPktData *pd = (DemuxPktData*)pkt->opaque_ref->data;
|
|
|
|
dts_est = pd->dts_est;
|
|
|
|
}
|
|
|
|
|
2023-04-03 13:47:12 +02:00
|
|
|
if (f->recording_time != INT64_MAX) {
|
|
|
|
int64_t start_time = 0;
|
|
|
|
if (copy_ts) {
|
|
|
|
start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time : 0;
|
|
|
|
start_time += start_at_zero ? 0 : f->start_time_effective;
|
|
|
|
}
|
2023-04-27 13:54:36 +02:00
|
|
|
if (dts_est >= f->recording_time + start_time)
|
2023-05-30 15:18:48 +02:00
|
|
|
pkt = NULL;
|
2023-04-03 13:47:12 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 12:47:03 +02:00
|
|
|
for (int oidx = 0; oidx < ist->nb_outputs; oidx++) {
|
|
|
|
OutputStream *ost = ist->outputs[oidx];
|
2023-04-20 09:06:25 +02:00
|
|
|
if (ost->enc || (!pkt && no_eof))
|
2011-11-21 15:48:45 +03:00
|
|
|
continue;
|
2004-04-08 21:54:40 +03:00
|
|
|
|
2023-07-12 19:52:19 +02:00
|
|
|
ret = of_streamcopy(ost, pkt, dts_est);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-11-21 15:48:45 +03:00
|
|
|
}
|
2003-01-23 20:44:26 +02:00
|
|
|
|
2016-10-01 17:22:15 +02:00
|
|
|
return !eof_reached;
|
2003-06-25 13:21:45 +03:00
|
|
|
}
|
|
|
|
|
2023-05-07 19:38:51 +02:00
|
|
|
static void print_stream_maps(void)
|
2011-07-27 21:56:59 +03:00
|
|
|
{
|
2011-09-13 08:31:21 +03:00
|
|
|
av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
|
2022-11-17 10:56:37 +02:00
|
|
|
for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
|
|
|
|
for (int j = 0; j < ist->nb_filters; j++) {
|
2016-08-04 05:33:57 +02:00
|
|
|
if (!filtergraph_is_simple(ist->filters[j]->graph)) {
|
2012-04-01 16:08:33 +03:00
|
|
|
av_log(NULL, AV_LOG_INFO, " Stream #%d:%d (%s) -> %s",
|
2023-05-19 09:53:57 +02:00
|
|
|
ist->file_index, ist->index, ist->dec ? ist->dec->name : "?",
|
2012-05-26 14:31:54 +03:00
|
|
|
ist->filters[j]->name);
|
2012-04-01 16:08:33 +03:00
|
|
|
if (nb_filtergraphs > 1)
|
|
|
|
av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index);
|
|
|
|
av_log(NULL, AV_LOG_INFO, "\n");
|
|
|
|
}
|
2003-11-10 20:54:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 10:56:37 +02:00
|
|
|
for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
|
2011-10-30 09:10:08 +03:00
|
|
|
if (ost->attachment_filename) {
|
|
|
|
/* an attached file */
|
|
|
|
av_log(NULL, AV_LOG_INFO, " File %s -> Stream #%d:%d\n",
|
|
|
|
ost->attachment_filename, ost->file_index, ost->index);
|
|
|
|
continue;
|
2011-11-04 03:44:06 +03:00
|
|
|
}
|
2011-10-18 02:33:09 +03:00
|
|
|
|
2016-08-04 05:33:57 +02:00
|
|
|
if (ost->filter && !filtergraph_is_simple(ost->filter->graph)) {
|
2012-04-01 16:08:33 +03:00
|
|
|
/* output from a complex graph */
|
2012-05-26 14:31:54 +03:00
|
|
|
av_log(NULL, AV_LOG_INFO, " %s", ost->filter->name);
|
2012-04-01 16:08:33 +03:00
|
|
|
if (nb_filtergraphs > 1)
|
|
|
|
av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
|
2011-10-18 02:33:09 +03:00
|
|
|
|
2012-04-01 16:08:33 +03:00
|
|
|
av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index,
|
2022-08-25 10:01:19 +02:00
|
|
|
ost->index, ost->enc_ctx->codec->name);
|
2011-10-18 02:33:09 +03:00
|
|
|
continue;
|
2001-09-17 00:47:13 +03:00
|
|
|
}
|
2011-10-26 03:09:31 +03:00
|
|
|
|
2011-11-06 15:00:13 +03:00
|
|
|
av_log(NULL, AV_LOG_INFO, " Stream #%d:%d -> #%d:%d",
|
2022-11-16 16:36:24 +02:00
|
|
|
ost->ist->file_index,
|
2023-05-19 09:53:57 +02:00
|
|
|
ost->ist->index,
|
2011-09-13 08:31:21 +03:00
|
|
|
ost->file_index,
|
|
|
|
ost->index);
|
2022-08-03 11:32:10 +02:00
|
|
|
if (ost->enc_ctx) {
|
2022-11-16 16:36:24 +02:00
|
|
|
const AVCodec *in_codec = ost->ist->dec;
|
2022-08-25 10:01:19 +02:00
|
|
|
const AVCodec *out_codec = ost->enc_ctx->codec;
|
2014-06-04 21:07:59 +03:00
|
|
|
const char *decoder_name = "?";
|
|
|
|
const char *in_codec_name = "?";
|
|
|
|
const char *encoder_name = "?";
|
|
|
|
const char *out_codec_name = "?";
|
2015-04-02 12:52:19 +02:00
|
|
|
const AVCodecDescriptor *desc;
|
2014-06-04 21:07:59 +03:00
|
|
|
|
|
|
|
if (in_codec) {
|
|
|
|
decoder_name = in_codec->name;
|
2015-04-02 12:52:19 +02:00
|
|
|
desc = avcodec_descriptor_get(in_codec->id);
|
|
|
|
if (desc)
|
|
|
|
in_codec_name = desc->name;
|
2014-06-04 21:07:59 +03:00
|
|
|
if (!strcmp(decoder_name, in_codec_name))
|
|
|
|
decoder_name = "native";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out_codec) {
|
|
|
|
encoder_name = out_codec->name;
|
2015-04-02 12:52:19 +02:00
|
|
|
desc = avcodec_descriptor_get(out_codec->id);
|
|
|
|
if (desc)
|
|
|
|
out_codec_name = desc->name;
|
2014-06-17 13:57:56 +03:00
|
|
|
if (!strcmp(encoder_name, out_codec_name))
|
2014-06-04 21:07:59 +03:00
|
|
|
encoder_name = "native";
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(NULL, AV_LOG_INFO, " (%s (%s) -> %s (%s))",
|
|
|
|
in_codec_name, decoder_name,
|
|
|
|
out_codec_name, encoder_name);
|
2022-08-03 11:32:10 +02:00
|
|
|
} else
|
|
|
|
av_log(NULL, AV_LOG_INFO, " (copy)");
|
2011-09-13 08:31:21 +03:00
|
|
|
av_log(NULL, AV_LOG_INFO, "\n");
|
2011-08-30 05:19:26 +03:00
|
|
|
}
|
2011-08-20 00:45:02 +03:00
|
|
|
}
|
2011-09-05 23:10:26 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
/**
|
2012-08-17 14:50:16 +03:00
|
|
|
* Select the output stream to process.
|
2012-08-09 01:26:38 +03:00
|
|
|
*
|
2023-04-29 22:35:37 +02:00
|
|
|
* @retval 0 an output stream was selected
|
|
|
|
* @retval AVERROR(EAGAIN) need to wait until more input is available
|
|
|
|
* @retval AVERROR_EOF no more streams need output
|
2012-08-09 01:26:38 +03:00
|
|
|
*/
|
2023-04-29 22:35:37 +02:00
|
|
|
static int choose_output(OutputStream **post)
|
2001-07-22 17:37:44 +03:00
|
|
|
{
|
2012-08-17 14:50:16 +03:00
|
|
|
int64_t opts_min = INT64_MAX;
|
|
|
|
OutputStream *ost_min = NULL;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2022-10-14 10:57:19 +02:00
|
|
|
for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
|
2022-06-10 14:38:32 +02:00
|
|
|
int64_t opts;
|
|
|
|
|
2023-03-27 08:12:14 +02:00
|
|
|
if (ost->filter && ost->filter->last_pts != AV_NOPTS_VALUE) {
|
|
|
|
opts = ost->filter->last_pts;
|
2022-06-10 14:38:32 +02:00
|
|
|
} else {
|
2022-06-10 14:45:48 +02:00
|
|
|
opts = ost->last_mux_dts == AV_NOPTS_VALUE ?
|
|
|
|
INT64_MIN : ost->last_mux_dts;
|
2022-06-10 14:38:32 +02:00
|
|
|
}
|
2015-11-11 18:19:12 +02:00
|
|
|
|
2023-04-29 22:35:37 +02:00
|
|
|
if (!ost->initialized && !ost->inputs_done && !ost->finished) {
|
|
|
|
ost_min = ost;
|
|
|
|
break;
|
|
|
|
}
|
2014-10-22 18:08:00 +03:00
|
|
|
if (!ost->finished && opts < opts_min) {
|
2012-08-17 14:50:16 +03:00
|
|
|
opts_min = opts;
|
2023-04-29 22:35:37 +02:00
|
|
|
ost_min = ost;
|
2012-01-17 17:25:14 +03:00
|
|
|
}
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
2023-04-29 22:35:37 +02:00
|
|
|
if (!ost_min)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
*post = ost_min;
|
|
|
|
return ost_min->unavailable ? AVERROR(EAGAIN) : 0;
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
2005-06-03 17:31:45 +03:00
|
|
|
|
2015-11-19 23:54:45 +02:00
|
|
|
static void set_tty_echo(int on)
|
|
|
|
{
|
|
|
|
#if HAVE_TERMIOS_H
|
|
|
|
struct termios tty;
|
|
|
|
if (tcgetattr(0, &tty) == 0) {
|
|
|
|
if (on) tty.c_lflag |= ECHO;
|
|
|
|
else tty.c_lflag &= ~ECHO;
|
|
|
|
tcsetattr(0, TCSANOW, &tty);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
static int check_keyboard_interaction(int64_t cur_time)
|
|
|
|
{
|
|
|
|
int i, ret, key;
|
|
|
|
static int64_t last_time;
|
|
|
|
if (received_nb_signals)
|
|
|
|
return AVERROR_EXIT;
|
|
|
|
/* read_key() returns 0 on EOF */
|
2022-04-03 17:24:40 +02:00
|
|
|
if (cur_time - last_time >= 100000) {
|
2012-08-09 01:26:38 +03:00
|
|
|
key = read_key();
|
|
|
|
last_time = cur_time;
|
|
|
|
}else
|
|
|
|
key = -1;
|
2021-10-13 10:34:10 +02:00
|
|
|
if (key == 'q') {
|
|
|
|
av_log(NULL, AV_LOG_INFO, "\n\n[q] command received. Exiting.\n\n");
|
2012-08-09 01:26:38 +03:00
|
|
|
return AVERROR_EXIT;
|
2021-10-13 10:34:10 +02:00
|
|
|
}
|
2012-08-09 01:26:38 +03:00
|
|
|
if (key == '+') av_log_set_level(av_log_get_level()+10);
|
|
|
|
if (key == '-') av_log_set_level(av_log_get_level()-10);
|
|
|
|
if (key == 'c' || key == 'C'){
|
|
|
|
char buf[4096], target[64], command[256], arg[256] = {0};
|
|
|
|
double time;
|
|
|
|
int k, n = 0;
|
2013-09-10 15:13:18 +03:00
|
|
|
fprintf(stderr, "\nEnter command: <target>|all <time>|-1 <command>[ <argument>]\n");
|
2012-08-09 01:26:38 +03:00
|
|
|
i = 0;
|
2015-11-19 23:54:45 +02:00
|
|
|
set_tty_echo(1);
|
2012-08-09 01:26:38 +03:00
|
|
|
while ((k = read_key()) != '\n' && k != '\r' && i < sizeof(buf)-1)
|
|
|
|
if (k > 0)
|
|
|
|
buf[i++] = k;
|
|
|
|
buf[i] = 0;
|
2015-11-19 23:54:45 +02:00
|
|
|
set_tty_echo(0);
|
|
|
|
fprintf(stderr, "\n");
|
2012-08-09 01:26:38 +03:00
|
|
|
if (k > 0 &&
|
|
|
|
(n = sscanf(buf, "%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
|
|
|
|
av_log(NULL, AV_LOG_DEBUG, "Processing command target:%s time:%f command:%s arg:%s",
|
|
|
|
target, time, command, arg);
|
|
|
|
for (i = 0; i < nb_filtergraphs; i++) {
|
|
|
|
FilterGraph *fg = filtergraphs[i];
|
|
|
|
if (fg->graph) {
|
|
|
|
if (time < 0) {
|
|
|
|
ret = avfilter_graph_send_command(fg->graph, target, command, arg, buf, sizeof(buf),
|
|
|
|
key == 'c' ? AVFILTER_CMD_FLAG_ONE : 0);
|
2013-09-10 14:57:29 +03:00
|
|
|
fprintf(stderr, "Command reply for stream %d: ret:%d res:\n%s", i, ret, buf);
|
2013-09-10 15:13:44 +03:00
|
|
|
} else if (key == 'c') {
|
2016-07-17 18:10:27 +02:00
|
|
|
fprintf(stderr, "Queuing commands only on filters supporting the specific command is unsupported\n");
|
2013-09-10 15:13:44 +03:00
|
|
|
ret = AVERROR_PATCHWELCOME;
|
2012-08-09 01:26:38 +03:00
|
|
|
} else {
|
|
|
|
ret = avfilter_graph_queue_command(fg->graph, target, command, arg, 0, time);
|
2015-05-15 12:41:28 +02:00
|
|
|
if (ret < 0)
|
2016-07-17 18:10:27 +02:00
|
|
|
fprintf(stderr, "Queuing command failed with error %s\n", av_err2str(ret));
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
|
|
|
}
|
2005-06-03 17:31:45 +03:00
|
|
|
}
|
2012-08-09 01:26:38 +03:00
|
|
|
} else {
|
|
|
|
av_log(NULL, AV_LOG_ERROR,
|
|
|
|
"Parse error, at least 3 arguments were expected, "
|
|
|
|
"only %d given in string '%s'\n", n, buf);
|
2005-06-03 17:31:45 +03:00
|
|
|
}
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
|
|
|
if (key == '?'){
|
|
|
|
fprintf(stderr, "key function\n"
|
|
|
|
"? show this help\n"
|
|
|
|
"+ increase verbosity\n"
|
|
|
|
"- decrease verbosity\n"
|
2013-09-10 15:13:18 +03:00
|
|
|
"c Send command to first matching filter supporting it\n"
|
2016-10-13 23:08:01 +02:00
|
|
|
"C Send/Queue command to all matching filters\n"
|
2012-08-09 01:26:38 +03:00
|
|
|
"h dump packets/hex press to cycle through the 3 states\n"
|
|
|
|
"q quit\n"
|
|
|
|
"s Show QP histogram\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return 0;
|
2005-06-03 17:31:45 +03:00
|
|
|
}
|
|
|
|
|
2012-08-04 13:04:02 +03:00
|
|
|
static void reset_eagain(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < nb_input_files; i++)
|
|
|
|
input_files[i]->eagain = 0;
|
2022-10-14 10:57:19 +02:00
|
|
|
for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost))
|
|
|
|
ost->unavailable = 0;
|
2012-08-17 14:50:16 +03:00
|
|
|
}
|
|
|
|
|
2022-03-22 11:41:55 +02:00
|
|
|
static void decode_flush(InputFile *ifile)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < ifile->nb_streams; i++) {
|
2022-11-17 10:49:27 +02:00
|
|
|
InputStream *ist = ifile->streams[i];
|
2022-03-22 11:41:55 +02:00
|
|
|
|
2023-06-02 09:59:31 +02:00
|
|
|
if (ist->discard || !ist->decoding_needed)
|
2022-03-22 11:41:55 +02:00
|
|
|
continue;
|
|
|
|
|
2023-06-02 09:59:31 +02:00
|
|
|
dec_packet(ist, NULL, 1);
|
2022-03-22 11:41:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 14:31:50 +03:00
|
|
|
/*
|
|
|
|
* Return
|
2012-08-09 19:04:17 +03:00
|
|
|
* - 0 -- one packet was read and processed
|
|
|
|
* - AVERROR(EAGAIN) -- no packets were available for selected file,
|
|
|
|
* this function should be called again
|
|
|
|
* - AVERROR_EOF -- this function should not be called again
|
|
|
|
*/
|
2012-08-17 14:50:16 +03:00
|
|
|
static int process_input(int file_index)
|
2012-08-09 19:04:17 +03:00
|
|
|
{
|
2012-08-17 14:50:16 +03:00
|
|
|
InputFile *ifile = input_files[file_index];
|
2012-08-09 19:04:17 +03:00
|
|
|
InputStream *ist;
|
2021-01-30 00:09:18 +02:00
|
|
|
AVPacket *pkt;
|
2022-10-14 10:57:19 +02:00
|
|
|
int ret, i;
|
2012-08-09 19:04:17 +03:00
|
|
|
|
2022-08-03 14:30:48 +02:00
|
|
|
ret = ifile_get_packet(ifile, &pkt);
|
2012-08-09 19:04:17 +03:00
|
|
|
|
|
|
|
if (ret == AVERROR(EAGAIN)) {
|
|
|
|
ifile->eagain = 1;
|
|
|
|
return ret;
|
|
|
|
}
|
2022-03-22 11:41:55 +02:00
|
|
|
if (ret == 1) {
|
|
|
|
/* the input file is looped: flush the decoders */
|
|
|
|
decode_flush(ifile);
|
|
|
|
return AVERROR(EAGAIN);
|
2015-10-14 14:28:20 +02:00
|
|
|
}
|
2012-08-09 19:04:17 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
if (ret != AVERROR_EOF) {
|
2023-05-07 18:29:09 +02:00
|
|
|
av_log(ifile, AV_LOG_ERROR,
|
|
|
|
"Error retrieving a packet from demuxer: %s\n", av_err2str(ret));
|
2012-08-09 19:04:17 +03:00
|
|
|
if (exit_on_error)
|
2023-07-12 19:48:05 +02:00
|
|
|
return ret;
|
2012-08-09 19:04:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ifile->nb_streams; i++) {
|
2022-11-17 10:49:27 +02:00
|
|
|
ist = ifile->streams[i];
|
2023-03-20 17:50:19 +02:00
|
|
|
if (!ist->discard) {
|
2015-10-14 14:28:20 +02:00
|
|
|
ret = process_input_packet(ist, NULL, 0);
|
2014-06-23 00:54:51 +03:00
|
|
|
if (ret>0)
|
|
|
|
return 0;
|
2023-07-12 19:49:43 +02:00
|
|
|
else if (ret < 0)
|
|
|
|
return ret;
|
2014-06-23 00:54:51 +03:00
|
|
|
}
|
2012-08-09 19:04:17 +03:00
|
|
|
|
2012-08-17 14:50:16 +03:00
|
|
|
/* mark all outputs that don't go through lavfi as finished */
|
2023-03-31 12:47:03 +02:00
|
|
|
for (int oidx = 0; oidx < ist->nb_outputs; oidx++) {
|
|
|
|
OutputStream *ost = ist->outputs[oidx];
|
|
|
|
OutputFile *of = output_files[ost->file_index];
|
2023-05-30 15:24:15 +02:00
|
|
|
|
2023-07-13 13:34:32 +02:00
|
|
|
ret = of_output_packet(of, ost, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-08-04 19:35:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 00:54:51 +03:00
|
|
|
ifile->eof_reached = 1;
|
2012-08-11 12:50:32 +03:00
|
|
|
return AVERROR(EAGAIN);
|
2012-08-09 19:04:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
reset_eagain();
|
|
|
|
|
2022-11-17 10:49:27 +02:00
|
|
|
ist = ifile->streams[pkt->stream_index];
|
2014-03-17 12:58:05 +03:00
|
|
|
|
2023-05-23 20:55:01 +02:00
|
|
|
sub2video_heartbeat(ifile, pkt->pts, pkt->time_base);
|
2012-08-09 19:04:17 +03:00
|
|
|
|
2023-07-12 19:49:43 +02:00
|
|
|
ret = process_input_packet(ist, pkt, 0);
|
2012-08-09 19:04:17 +03:00
|
|
|
|
2022-03-21 16:53:38 +02:00
|
|
|
av_packet_free(&pkt);
|
2012-08-09 19:04:17 +03:00
|
|
|
|
2023-07-12 19:49:43 +02:00
|
|
|
return ret < 0 ? ret : 0;
|
2012-08-09 19:04:17 +03:00
|
|
|
}
|
|
|
|
|
2012-08-17 14:50:16 +03:00
|
|
|
/**
|
|
|
|
* Run a single step of transcoding.
|
|
|
|
*
|
|
|
|
* @return 0 for success, <0 for error
|
|
|
|
*/
|
2023-04-29 22:46:14 +02:00
|
|
|
static int transcode_step(OutputStream *ost)
|
2012-08-17 14:50:16 +03:00
|
|
|
{
|
2016-05-27 12:14:33 +02:00
|
|
|
InputStream *ist = NULL;
|
2012-08-17 14:50:16 +03:00
|
|
|
int ret;
|
|
|
|
|
2023-04-30 22:50:40 +02:00
|
|
|
if (ost->filter) {
|
2023-04-18 15:24:46 +02:00
|
|
|
if ((ret = fg_transcode_step(ost->filter->graph, &ist)) < 0)
|
2012-08-17 14:50:16 +03:00
|
|
|
return ret;
|
|
|
|
if (!ist)
|
|
|
|
return 0;
|
|
|
|
} else {
|
2022-11-16 16:36:24 +02:00
|
|
|
ist = ost->ist;
|
|
|
|
av_assert0(ist);
|
2012-08-17 14:50:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = process_input(ist->file_index);
|
|
|
|
if (ret == AVERROR(EAGAIN)) {
|
|
|
|
if (input_files[ist->file_index]->eagain)
|
|
|
|
ost->unavailable = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-19 13:41:16 +02:00
|
|
|
|
2012-08-17 14:50:16 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret == AVERROR_EOF ? 0 : ret;
|
|
|
|
|
2023-06-08 13:28:33 +02:00
|
|
|
// process_input() above might have caused output to become available
|
|
|
|
// in multiple filtergraphs, so we process all of them
|
|
|
|
for (int i = 0; i < nb_filtergraphs; i++) {
|
|
|
|
ret = reap_filters(filtergraphs[i], 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-08-17 14:50:16 +03:00
|
|
|
}
|
|
|
|
|
2011-08-20 00:45:02 +03:00
|
|
|
/*
|
|
|
|
* The following code is the main loop of the file converter
|
|
|
|
*/
|
2023-05-16 20:21:20 +02:00
|
|
|
static int transcode(int *err_rate_exceeded)
|
2005-06-03 17:31:45 +03:00
|
|
|
{
|
2023-05-16 20:21:20 +02:00
|
|
|
int ret = 0, i;
|
2011-08-20 00:45:02 +03:00
|
|
|
InputStream *ist;
|
|
|
|
int64_t timer_start;
|
2001-07-22 17:37:44 +03:00
|
|
|
|
2023-05-07 19:38:51 +02:00
|
|
|
print_stream_maps();
|
|
|
|
|
2023-05-16 20:21:20 +02:00
|
|
|
*err_rate_exceeded = 0;
|
2023-05-07 19:38:51 +02:00
|
|
|
atomic_store(&transcode_init_done, 1);
|
2011-08-20 00:45:02 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
if (stdin_interaction) {
|
|
|
|
av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");
|
2012-04-01 16:08:33 +03:00
|
|
|
}
|
|
|
|
|
2014-05-07 00:16:47 +03:00
|
|
|
timer_start = av_gettime_relative();
|
2001-07-22 17:37:44 +03:00
|
|
|
|
2012-08-04 13:06:30 +03:00
|
|
|
while (!received_sigterm) {
|
2023-04-29 22:46:14 +02:00
|
|
|
OutputStream *ost;
|
2014-05-07 00:16:47 +03:00
|
|
|
int64_t cur_time= av_gettime_relative();
|
2012-04-01 16:08:33 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
/* if 'q' pressed, exits */
|
|
|
|
if (stdin_interaction)
|
|
|
|
if (check_keyboard_interaction(cur_time) < 0)
|
|
|
|
break;
|
2011-07-27 21:56:59 +03:00
|
|
|
|
2023-04-29 22:46:14 +02:00
|
|
|
ret = choose_output(&ost);
|
|
|
|
if (ret == AVERROR(EAGAIN)) {
|
|
|
|
reset_eagain();
|
|
|
|
av_usleep(10000);
|
|
|
|
continue;
|
|
|
|
} else if (ret < 0) {
|
2012-06-01 11:44:11 +03:00
|
|
|
av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
|
2023-04-29 22:46:14 +02:00
|
|
|
ret = 0;
|
2012-04-01 16:08:33 +03:00
|
|
|
break;
|
2012-06-01 11:44:11 +03:00
|
|
|
}
|
2012-04-01 16:08:33 +03:00
|
|
|
|
2023-04-29 22:46:14 +02:00
|
|
|
ret = transcode_step(ost);
|
2016-01-19 09:41:32 +02:00
|
|
|
if (ret < 0 && ret != AVERROR_EOF) {
|
2018-02-26 09:38:53 +02:00
|
|
|
av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
|
2016-01-19 09:40:32 +02:00
|
|
|
break;
|
2011-10-29 21:25:39 +03:00
|
|
|
}
|
2011-07-27 21:56:59 +03:00
|
|
|
|
|
|
|
/* dump report by using the output first video and audio streams */
|
2012-08-09 01:26:38 +03:00
|
|
|
print_report(0, timer_start, cur_time);
|
2001-07-22 17:37:44 +03:00
|
|
|
}
|
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
/* at the end of stream, we must flush the decoder buffers */
|
2022-11-17 10:49:27 +02:00
|
|
|
for (ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
|
2023-05-16 20:21:20 +02:00
|
|
|
float err_rate;
|
|
|
|
|
2017-10-31 04:07:15 +02:00
|
|
|
if (!input_files[ist->file_index]->eof_reached) {
|
2023-07-12 19:49:43 +02:00
|
|
|
int err = process_input_packet(ist, NULL, 0);
|
|
|
|
ret = err_merge(ret, err);
|
2002-05-20 19:32:49 +03:00
|
|
|
}
|
2023-05-16 20:21:20 +02:00
|
|
|
|
|
|
|
err_rate = (ist->frames_decoded || ist->decode_errors) ?
|
|
|
|
ist->decode_errors / (ist->frames_decoded + ist->decode_errors) : 0.f;
|
|
|
|
if (err_rate > max_error_rate) {
|
|
|
|
av_log(ist, AV_LOG_FATAL, "Decode error rate %g exceeds maximum %g\n",
|
|
|
|
err_rate, max_error_rate);
|
|
|
|
*err_rate_exceeded = 1;
|
|
|
|
} else if (err_rate)
|
|
|
|
av_log(ist, AV_LOG_VERBOSE, "Decode error rate %g\n", err_rate);
|
2001-09-17 00:47:13 +03:00
|
|
|
}
|
2023-07-13 13:34:32 +02:00
|
|
|
ret = err_merge(ret, enc_flush());
|
2001-09-17 00:47:13 +03:00
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
term_exit();
|
2001-07-22 17:37:44 +03:00
|
|
|
|
2021-11-30 02:18:14 +02:00
|
|
|
/* write the trailer if needed */
|
2011-12-29 23:28:29 +03:00
|
|
|
for (i = 0; i < nb_output_files; i++) {
|
2023-03-28 09:59:24 +02:00
|
|
|
int err = of_write_trailer(output_files[i]);
|
|
|
|
ret = err_merge(ret, err);
|
2011-10-26 03:09:31 +03:00
|
|
|
}
|
2004-10-07 01:29:30 +03:00
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
/* dump report by using the first video and audio streams */
|
2014-05-07 00:16:47 +03:00
|
|
|
print_report(1, timer_start, av_gettime_relative());
|
2011-06-15 09:00:03 +03:00
|
|
|
|
2023-03-28 09:59:24 +02:00
|
|
|
return ret;
|
2001-07-22 17:37:44 +03:00
|
|
|
}
|
|
|
|
|
2018-04-27 19:31:59 +02:00
|
|
|
static BenchmarkTimeStamps get_benchmark_time_stamps(void)
|
2002-10-10 20:09:01 +03:00
|
|
|
{
|
2018-04-30 18:19:29 +02:00
|
|
|
BenchmarkTimeStamps time_stamps = { av_gettime_relative() };
|
2012-08-01 19:23:12 +03:00
|
|
|
#if HAVE_GETRUSAGE
|
|
|
|
struct rusage rusage;
|
2011-11-02 23:50:54 +03:00
|
|
|
|
2012-08-01 19:23:12 +03:00
|
|
|
getrusage(RUSAGE_SELF, &rusage);
|
2018-04-27 19:31:59 +02:00
|
|
|
time_stamps.user_usec =
|
|
|
|
(rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
|
|
|
|
time_stamps.sys_usec =
|
|
|
|
(rusage.ru_stime.tv_sec * 1000000LL) + rusage.ru_stime.tv_usec;
|
2012-08-01 19:23:12 +03:00
|
|
|
#elif HAVE_GETPROCESSTIMES
|
|
|
|
HANDLE proc;
|
|
|
|
FILETIME c, e, k, u;
|
|
|
|
proc = GetCurrentProcess();
|
|
|
|
GetProcessTimes(proc, &c, &e, &k, &u);
|
2018-04-27 19:31:59 +02:00
|
|
|
time_stamps.user_usec =
|
|
|
|
((int64_t)u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
|
|
|
|
time_stamps.sys_usec =
|
|
|
|
((int64_t)k.dwHighDateTime << 32 | k.dwLowDateTime) / 10;
|
2012-08-01 19:23:12 +03:00
|
|
|
#else
|
2018-04-27 19:31:59 +02:00
|
|
|
time_stamps.user_usec = time_stamps.sys_usec = 0;
|
2012-08-01 19:23:12 +03:00
|
|
|
#endif
|
2018-04-27 19:31:59 +02:00
|
|
|
return time_stamps;
|
2002-10-10 20:09:01 +03:00
|
|
|
}
|
2001-09-25 02:22:25 +03:00
|
|
|
|
2010-02-23 00:21:58 +02:00
|
|
|
static int64_t getmaxrss(void)
|
|
|
|
{
|
|
|
|
#if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
|
|
|
|
struct rusage rusage;
|
|
|
|
getrusage(RUSAGE_SELF, &rusage);
|
|
|
|
return (int64_t)rusage.ru_maxrss * 1024;
|
|
|
|
#elif HAVE_GETPROCESSMEMORYINFO
|
|
|
|
HANDLE proc;
|
|
|
|
PROCESS_MEMORY_COUNTERS memcounters;
|
|
|
|
proc = GetCurrentProcess();
|
|
|
|
memcounters.cb = sizeof(memcounters);
|
|
|
|
GetProcessMemoryInfo(proc, &memcounters, sizeof(memcounters));
|
|
|
|
return memcounters.PeakPagefileUsage;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-09-16 21:08:51 +03:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2023-05-16 20:21:20 +02:00
|
|
|
int ret, err_rate_exceeded;
|
2018-04-27 19:31:59 +02:00
|
|
|
BenchmarkTimeStamps ti;
|
2007-09-16 21:08:51 +03:00
|
|
|
|
2016-08-08 15:27:41 +02:00
|
|
|
init_dynload();
|
|
|
|
|
2012-09-30 16:02:03 +03:00
|
|
|
setvbuf(stderr,NULL,_IONBF,0); /* win32 runtime needs this */
|
|
|
|
|
2010-09-24 18:39:10 +03:00
|
|
|
av_log_set_flags(AV_LOG_SKIP_REPEATED);
|
2011-09-26 09:15:37 +03:00
|
|
|
parse_loglevel(argc, argv, options);
|
2010-09-24 18:39:10 +03:00
|
|
|
|
2010-04-22 11:55:23 +03:00
|
|
|
#if CONFIG_AVDEVICE
|
2007-11-22 18:10:02 +02:00
|
|
|
avdevice_register_all();
|
2010-05-07 12:43:21 +03:00
|
|
|
#endif
|
2011-11-06 03:47:48 +03:00
|
|
|
avformat_network_init();
|
2007-09-16 21:08:51 +03:00
|
|
|
|
2011-12-21 00:03:25 +03:00
|
|
|
show_banner(argc, argv, options);
|
2007-09-16 21:08:51 +03:00
|
|
|
|
2012-06-08 22:35:16 +03:00
|
|
|
/* parse options and open all input/output files */
|
2012-12-20 04:38:02 +03:00
|
|
|
ret = ffmpeg_parse_options(argc, argv);
|
2012-06-08 22:35:16 +03:00
|
|
|
if (ret < 0)
|
2023-07-14 18:10:58 +02:00
|
|
|
goto finish;
|
2007-09-16 21:08:51 +03:00
|
|
|
|
2011-12-30 05:46:24 +03:00
|
|
|
if (nb_output_files <= 0 && nb_input_files == 0) {
|
2009-12-21 04:15:46 +02:00
|
|
|
show_usage();
|
2011-09-27 03:14:37 +03:00
|
|
|
av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
|
2023-07-14 18:10:58 +02:00
|
|
|
ret = 1;
|
|
|
|
goto finish;
|
2009-12-21 04:15:46 +02:00
|
|
|
}
|
2009-12-13 15:49:14 +02:00
|
|
|
|
2007-09-16 21:08:51 +03:00
|
|
|
if (nb_output_files <= 0) {
|
2011-09-27 03:14:37 +03:00
|
|
|
av_log(NULL, AV_LOG_FATAL, "At least one output file must be specified\n");
|
2023-07-14 18:10:58 +02:00
|
|
|
ret = 1;
|
|
|
|
goto finish;
|
2007-09-16 21:08:51 +03:00
|
|
|
}
|
|
|
|
|
2018-04-27 19:31:59 +02:00
|
|
|
current_time = ti = get_benchmark_time_stamps();
|
2023-05-16 20:21:20 +02:00
|
|
|
ret = transcode(&err_rate_exceeded);
|
2023-03-28 09:59:24 +02:00
|
|
|
if (ret >= 0 && do_benchmark) {
|
2018-04-30 18:19:29 +02:00
|
|
|
int64_t utime, stime, rtime;
|
2018-04-27 19:31:59 +02:00
|
|
|
current_time = get_benchmark_time_stamps();
|
2018-04-30 18:19:29 +02:00
|
|
|
utime = current_time.user_usec - ti.user_usec;
|
|
|
|
stime = current_time.sys_usec - ti.sys_usec;
|
|
|
|
rtime = current_time.real_usec - ti.real_usec;
|
2018-04-27 19:31:59 +02:00
|
|
|
av_log(NULL, AV_LOG_INFO,
|
|
|
|
"bench: utime=%0.3fs stime=%0.3fs rtime=%0.3fs\n",
|
|
|
|
utime / 1000000.0, stime / 1000000.0, rtime / 1000000.0);
|
2007-09-16 21:08:51 +03:00
|
|
|
}
|
|
|
|
|
2023-05-16 20:21:20 +02:00
|
|
|
ret = received_nb_signals ? 255 :
|
|
|
|
err_rate_exceeded ? 69 : ret;
|
|
|
|
|
2023-07-14 18:10:58 +02:00
|
|
|
finish:
|
2023-07-14 18:15:27 +02:00
|
|
|
if (ret == AVERROR_EXIT)
|
|
|
|
ret = 0;
|
|
|
|
|
2023-07-14 18:36:05 +02:00
|
|
|
ffmpeg_cleanup(ret);
|
2023-03-28 09:59:24 +02:00
|
|
|
return ret;
|
2007-09-16 21:08:51 +03:00
|
|
|
}
|