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
|
|
|
|
2023-03-25 12:49:54 +02:00
|
|
|
int64_t nb_frames_dup = 0;
|
|
|
|
int64_t nb_frames_drop = 0;
|
2013-04-10 02:10:06 +03:00
|
|
|
static int64_t decode_error_stat[2];
|
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.
|
|
|
|
*/
|
|
|
|
|
2013-03-10 13:51:02 +03:00
|
|
|
static int sub2video_get_blank_frame(InputStream *ist)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
AVFrame *frame = ist->sub2video.frame;
|
|
|
|
|
|
|
|
av_frame_unref(frame);
|
2015-08-15 17:28:21 +02:00
|
|
|
ist->sub2video.frame->width = ist->dec_ctx->width ? ist->dec_ctx->width : ist->sub2video.w;
|
|
|
|
ist->sub2video.frame->height = ist->dec_ctx->height ? ist->dec_ctx->height : ist->sub2video.h;
|
2013-03-10 13:51:02 +03:00
|
|
|
ist->sub2video.frame->format = AV_PIX_FMT_RGB32;
|
2017-02-08 10:51:17 +02:00
|
|
|
if ((ret = av_frame_get_buffer(frame, 0)) < 0)
|
2013-03-10 13:51:02 +03:00
|
|
|
return ret;
|
|
|
|
memset(frame->data[0], 0, frame->height * frame->linesize[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-07-26 20:29:27 +03:00
|
|
|
|
|
|
|
static void sub2video_copy_rect(uint8_t *dst, int dst_linesize, int w, int h,
|
|
|
|
AVSubtitleRect *r)
|
|
|
|
{
|
|
|
|
uint32_t *pal, *dst2;
|
|
|
|
uint8_t *src, *src2;
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
if (r->type != SUBTITLE_BITMAP) {
|
|
|
|
av_log(NULL, AV_LOG_WARNING, "sub2video: non-bitmap subtitle\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) {
|
2015-08-13 00:45:06 +02:00
|
|
|
av_log(NULL, AV_LOG_WARNING, "sub2video: rectangle (%d %d %d %d) overflowing %d %d\n",
|
|
|
|
r->x, r->y, r->w, r->h, w, h
|
|
|
|
);
|
2012-07-26 20:29:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst += r->y * dst_linesize + r->x * 4;
|
2016-06-25 13:35:46 +02:00
|
|
|
src = r->data[0];
|
|
|
|
pal = (uint32_t *)r->data[1];
|
2012-07-26 20:29:27 +03:00
|
|
|
for (y = 0; y < r->h; y++) {
|
|
|
|
dst2 = (uint32_t *)dst;
|
|
|
|
src2 = src;
|
|
|
|
for (x = 0; x < r->w; x++)
|
|
|
|
*(dst2++) = pal[*(src2++)];
|
|
|
|
dst += dst_linesize;
|
2016-06-25 13:35:46 +02:00
|
|
|
src += r->linesize[0];
|
2012-07-26 20:29:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sub2video_push_ref(InputStream *ist, int64_t pts)
|
|
|
|
{
|
2013-03-10 13:51:02 +03:00
|
|
|
AVFrame *frame = ist->sub2video.frame;
|
2012-07-26 20:29:27 +03:00
|
|
|
int i;
|
2017-11-18 07:24:24 +02:00
|
|
|
int ret;
|
2012-07-26 20:29:27 +03:00
|
|
|
|
2013-03-10 13:51:02 +03:00
|
|
|
av_assert1(frame->data[0]);
|
|
|
|
ist->sub2video.last_pts = frame->pts = pts;
|
2017-11-18 07:24:24 +02:00
|
|
|
for (i = 0; i < ist->nb_filters; i++) {
|
|
|
|
ret = av_buffersrc_add_frame_flags(ist->filters[i]->filter, frame,
|
|
|
|
AV_BUFFERSRC_FLAG_KEEP_REF |
|
|
|
|
AV_BUFFERSRC_FLAG_PUSH);
|
|
|
|
if (ret != AVERROR_EOF && ret < 0)
|
|
|
|
av_log(NULL, AV_LOG_WARNING, "Error while add the frame to buffer source(%s).\n",
|
|
|
|
av_err2str(ret));
|
|
|
|
}
|
2012-07-26 20:29:27 +03:00
|
|
|
}
|
|
|
|
|
2019-02-23 01:48:08 +02:00
|
|
|
void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub)
|
2012-07-26 20:29:27 +03:00
|
|
|
{
|
2013-03-10 13:51:02 +03:00
|
|
|
AVFrame *frame = ist->sub2video.frame;
|
2012-07-26 20:29:27 +03:00
|
|
|
int8_t *dst;
|
|
|
|
int dst_linesize;
|
2012-11-29 22:25:37 +03:00
|
|
|
int num_rects, i;
|
|
|
|
int64_t pts, end_pts;
|
2012-07-26 20:29:27 +03:00
|
|
|
|
2013-03-10 13:51:02 +03:00
|
|
|
if (!frame)
|
2012-07-26 20:29:27 +03:00
|
|
|
return;
|
2012-11-29 22:25:37 +03:00
|
|
|
if (sub) {
|
2014-07-12 03:11:02 +03:00
|
|
|
pts = av_rescale_q(sub->pts + sub->start_display_time * 1000LL,
|
2012-11-29 22:25:37 +03:00
|
|
|
AV_TIME_BASE_Q, ist->st->time_base);
|
2014-07-12 03:11:02 +03:00
|
|
|
end_pts = av_rescale_q(sub->pts + sub->end_display_time * 1000LL,
|
2012-11-29 22:25:37 +03:00
|
|
|
AV_TIME_BASE_Q, ist->st->time_base);
|
|
|
|
num_rects = sub->num_rects;
|
|
|
|
} else {
|
2019-02-23 01:48:08 +02:00
|
|
|
/* If we are initializing the system, utilize current heartbeat
|
|
|
|
PTS as the start time, and show until the following subpicture
|
|
|
|
is received. Otherwise, utilize the previous subpicture's end time
|
|
|
|
as the fall-back value. */
|
|
|
|
pts = ist->sub2video.initialize ?
|
|
|
|
heartbeat_pts : ist->sub2video.end_pts;
|
2012-11-29 22:25:37 +03:00
|
|
|
end_pts = INT64_MAX;
|
|
|
|
num_rects = 0;
|
|
|
|
}
|
2013-03-10 13:51:02 +03:00
|
|
|
if (sub2video_get_blank_frame(ist) < 0) {
|
2022-06-20 09:53:58 +02:00
|
|
|
av_log(NULL, AV_LOG_ERROR,
|
2013-03-10 13:51:02 +03:00
|
|
|
"Impossible to get a blank canvas.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dst = frame->data [0];
|
|
|
|
dst_linesize = frame->linesize[0];
|
2012-11-29 22:25:37 +03:00
|
|
|
for (i = 0; i < num_rects; i++)
|
2015-08-15 17:28:16 +02:00
|
|
|
sub2video_copy_rect(dst, dst_linesize, frame->width, frame->height, sub->rects[i]);
|
2012-07-26 20:29:27 +03:00
|
|
|
sub2video_push_ref(ist, pts);
|
2012-11-29 22:25:37 +03:00
|
|
|
ist->sub2video.end_pts = end_pts;
|
2019-02-23 01:48:08 +02:00
|
|
|
ist->sub2video.initialize = 0;
|
2012-07-26 20:29:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sub2video_heartbeat(InputStream *ist, int64_t pts)
|
|
|
|
{
|
|
|
|
InputFile *infile = input_files[ist->file_index];
|
|
|
|
int i, j, nb_reqs;
|
|
|
|
int64_t pts2;
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
for (i = 0; i < infile->nb_streams; i++) {
|
2022-11-17 10:49:27 +02:00
|
|
|
InputStream *ist2 = infile->streams[i];
|
2013-03-10 13:51:02 +03:00
|
|
|
if (!ist2->sub2video.frame)
|
2012-07-26 20:29:27 +03:00
|
|
|
continue;
|
|
|
|
/* subtitles seem to be usually muxed ahead of other streams;
|
2014-06-03 05:46:04 +03:00
|
|
|
if not, subtracting a larger time here is necessary */
|
2012-07-26 20:29:27 +03:00
|
|
|
pts2 = av_rescale_q(pts, ist->st->time_base, ist2->st->time_base) - 1;
|
|
|
|
/* do not send the heartbeat frame if the subtitle is already ahead */
|
|
|
|
if (pts2 <= ist2->sub2video.last_pts)
|
|
|
|
continue;
|
2019-02-23 01:48:08 +02:00
|
|
|
if (pts2 >= ist2->sub2video.end_pts || ist2->sub2video.initialize)
|
|
|
|
/* if we have hit the end of the current displayed subpicture,
|
|
|
|
or if we need to initialize the system, update the
|
|
|
|
overlayed subpicture and its start/end times */
|
|
|
|
sub2video_update(ist2, pts2 + 1, NULL);
|
2012-07-26 20:29:27 +03:00
|
|
|
for (j = 0, nb_reqs = 0; j < ist2->nb_filters; j++)
|
|
|
|
nb_reqs += av_buffersrc_get_nb_failed_requests(ist2->filters[j]->filter);
|
|
|
|
if (nb_reqs)
|
|
|
|
sub2video_push_ref(ist2, pts2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sub2video_flush(InputStream *ist)
|
|
|
|
{
|
|
|
|
int i;
|
2017-11-18 07:24:24 +02:00
|
|
|
int ret;
|
2012-07-26 20:29:27 +03:00
|
|
|
|
2014-03-02 22:59:32 +03:00
|
|
|
if (ist->sub2video.end_pts < INT64_MAX)
|
2019-02-23 01:48:08 +02:00
|
|
|
sub2video_update(ist, INT64_MAX, NULL);
|
2017-11-18 07:24:24 +02:00
|
|
|
for (i = 0; i < ist->nb_filters; i++) {
|
|
|
|
ret = av_buffersrc_add_frame(ist->filters[i]->filter, NULL);
|
|
|
|
if (ret != AVERROR_EOF && ret < 0)
|
|
|
|
av_log(NULL, AV_LOG_WARNING, "Flush the frame error.\n");
|
|
|
|
}
|
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;
|
|
|
|
int ist_idx = prev ? prev->st->index + 1 : 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 19:23:12 +03:00
|
|
|
void assert_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);
|
2013-07-07 02:52:51 +03:00
|
|
|
exit_program(1);
|
2012-04-01 16:08:33 +03:00
|
|
|
}
|
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;
|
2015-10-09 16:39:27 +02:00
|
|
|
int64_t pts = INT64_MIN + 1;
|
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;
|
2012-08-09 01:26:38 +03:00
|
|
|
int hours, mins, secs, us;
|
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
|
|
|
|
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) {
|
|
|
|
pts = FFMAX(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
|
|
|
}
|
|
|
|
|
2015-02-10 23:09:29 +02:00
|
|
|
secs = FFABS(pts) / AV_TIME_BASE;
|
|
|
|
us = FFABS(pts) % AV_TIME_BASE;
|
2012-08-09 01:26:38 +03:00
|
|
|
mins = secs / 60;
|
|
|
|
secs %= 60;
|
|
|
|
hours = mins / 60;
|
|
|
|
mins %= 60;
|
2018-02-12 12:11:23 +02:00
|
|
|
hours_sign = (pts < 0) ? "-" : "";
|
2010-02-03 00:23:09 +02:00
|
|
|
|
2012-12-23 02:19:11 +03:00
|
|
|
bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
|
2015-12-11 17:49:14 +02:00
|
|
|
speed = 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 {
|
2018-02-26 16:13:29 +02:00
|
|
|
av_bprintf(&buf, "%s%02d:%02d:%02d.%02d ",
|
|
|
|
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);
|
|
|
|
av_bprintf(&buf_script, "out_time=%s%02d:%02d:%02d.%06d\n",
|
|
|
|
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-03-25 12:49:54 +02:00
|
|
|
int ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par)
|
2018-03-18 19:04:13 +02:00
|
|
|
{
|
2021-09-03 02:04:30 +02:00
|
|
|
int ret;
|
|
|
|
|
2018-03-18 19:04:13 +02:00
|
|
|
// We never got any input. Set a fake format, which will
|
|
|
|
// come from libavformat.
|
|
|
|
ifilter->format = par->format;
|
|
|
|
ifilter->sample_rate = par->sample_rate;
|
|
|
|
ifilter->width = par->width;
|
|
|
|
ifilter->height = par->height;
|
|
|
|
ifilter->sample_aspect_ratio = par->sample_aspect_ratio;
|
2021-09-03 02:04:30 +02:00
|
|
|
ret = av_channel_layout_copy(&ifilter->ch_layout, &par->ch_layout);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
2018-03-18 19:04:13 +02:00
|
|
|
}
|
|
|
|
|
2015-10-15 00:48:24 +02:00
|
|
|
static void check_decode_result(InputStream *ist, int *got_output, int ret)
|
2015-10-15 00:36:38 +02:00
|
|
|
{
|
|
|
|
if (*got_output || ret<0)
|
|
|
|
decode_error_stat[ret<0] ++;
|
|
|
|
|
|
|
|
if (ret < 0 && exit_on_error)
|
|
|
|
exit_program(1);
|
2015-10-15 00:48:24 +02:00
|
|
|
|
2018-10-09 23:03:20 +02:00
|
|
|
if (*got_output && ist) {
|
2017-04-22 10:58:03 +02:00
|
|
|
if (ist->decoded_frame->decode_error_flags || (ist->decoded_frame->flags & AV_FRAME_FLAG_CORRUPT)) {
|
2018-10-09 23:03:20 +02:00
|
|
|
av_log(NULL, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
|
|
|
|
"%s: corrupt decoded frame in stream %d\n", input_files[ist->file_index]->ctx->url, ist->st->index);
|
|
|
|
if (exit_on_error)
|
|
|
|
exit_program(1);
|
2015-10-15 00:48:24 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-15 00:36:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-27 12:14:33 +02:00
|
|
|
// Filters can be configured only if the formats of all inputs are known.
|
2023-03-25 12:49:54 +02:00
|
|
|
int ifilter_has_all_input_formats(FilterGraph *fg)
|
2016-05-27 12:14:33 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < fg->nb_inputs; i++) {
|
|
|
|
if (fg->inputs[i]->format < 0 && (fg->inputs[i]->type == AVMEDIA_TYPE_AUDIO ||
|
|
|
|
fg->inputs[i]->type == AVMEDIA_TYPE_VIDEO))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-10-01 17:22:15 +02:00
|
|
|
// This does not quite work like avcodec_decode_audio4/avcodec_decode_video2.
|
|
|
|
// There is the following difference: if you got a frame, you must call
|
2017-07-06 18:42:24 +02:00
|
|
|
// it again with pkt=NULL. pkt==NULL is treated differently from pkt->size==0
|
|
|
|
// (pkt==NULL means get more output, pkt->size==0 is a flush/drain packet)
|
2023-01-27 13:19:16 +02:00
|
|
|
static int decode(InputStream *ist, AVCodecContext *avctx,
|
2023-04-26 10:05:26 +02:00
|
|
|
AVFrame *frame, int *got_frame, const AVPacket *pkt)
|
2016-10-01 17:22:15 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*got_frame = 0;
|
|
|
|
|
|
|
|
if (pkt) {
|
|
|
|
ret = avcodec_send_packet(avctx, pkt);
|
|
|
|
// In particular, we don't expect AVERROR(EAGAIN), because we read all
|
|
|
|
// decoded frames with avcodec_receive_frame() until done.
|
|
|
|
if (ret < 0 && ret != AVERROR_EOF)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avcodec_receive_frame(avctx, frame);
|
|
|
|
if (ret < 0 && ret != AVERROR(EAGAIN))
|
|
|
|
return ret;
|
2023-01-27 13:19:16 +02:00
|
|
|
if (ret >= 0) {
|
|
|
|
if (ist->want_frame_data) {
|
|
|
|
FrameData *fd;
|
|
|
|
|
|
|
|
av_assert0(!frame->opaque_ref);
|
|
|
|
frame->opaque_ref = av_buffer_allocz(sizeof(*fd));
|
|
|
|
if (!frame->opaque_ref) {
|
|
|
|
av_frame_unref(frame);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
fd = (FrameData*)frame->opaque_ref->data;
|
|
|
|
fd->pts = frame->pts;
|
|
|
|
fd->tb = avctx->pkt_timebase;
|
2023-01-24 01:35:54 +02:00
|
|
|
fd->idx = avctx->frame_num - 1;
|
2023-01-27 13:19:16 +02:00
|
|
|
}
|
|
|
|
|
2023-04-22 16:42:12 +02:00
|
|
|
frame->time_base = avctx->pkt_timebase;
|
|
|
|
|
2016-10-01 17:22:15 +02:00
|
|
|
*got_frame = 1;
|
2023-01-27 13:19:16 +02:00
|
|
|
}
|
2016-10-01 17:22:15 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-24 23:01:23 +02:00
|
|
|
static int send_frame_to_filters(InputStream *ist, AVFrame *decoded_frame)
|
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
|
2016-11-24 16:45:00 +02:00
|
|
|
av_assert1(ist->nb_filters > 0); /* ensure ret is initialized */
|
2016-10-24 23:01:23 +02:00
|
|
|
for (i = 0; i < ist->nb_filters; i++) {
|
2021-11-19 18:29:33 +02:00
|
|
|
ret = ifilter_send_frame(ist->filters[i], decoded_frame, i < ist->nb_filters - 1);
|
2016-10-24 23:01:23 +02:00
|
|
|
if (ret == AVERROR_EOF)
|
|
|
|
ret = 0; /* ignore */
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR,
|
|
|
|
"Failed to inject frame into filter network: %s\n", av_err2str(ret));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-04-24 12:28:13 +02:00
|
|
|
static AVRational audio_samplerate_update(InputStream *ist, const AVFrame *frame)
|
|
|
|
{
|
|
|
|
const int prev = ist->last_frame_tb.den;
|
|
|
|
const int sr = frame->sample_rate;
|
|
|
|
|
|
|
|
AVRational tb_new;
|
|
|
|
int64_t gcd;
|
|
|
|
|
|
|
|
if (frame->sample_rate == ist->last_frame_sample_rate)
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
gcd = av_gcd(prev, sr);
|
|
|
|
|
|
|
|
if (prev / gcd >= INT_MAX / sr) {
|
|
|
|
av_log(ist, AV_LOG_WARNING,
|
|
|
|
"Audio timestamps cannot be represented exactly after "
|
|
|
|
"sample rate change: %d -> %d\n", prev, sr);
|
|
|
|
|
|
|
|
// LCM of 192000, 44100, allows to represent all common samplerates
|
|
|
|
tb_new = (AVRational){ 1, 28224000 };
|
|
|
|
} else
|
|
|
|
tb_new = (AVRational){ 1, prev / gcd * sr };
|
|
|
|
|
|
|
|
// keep the frame timebase if it is strictly better than
|
|
|
|
// the samplerate-defined one
|
|
|
|
if (frame->time_base.num == 1 && frame->time_base.den > tb_new.den &&
|
|
|
|
!(frame->time_base.den % tb_new.den))
|
|
|
|
tb_new = frame->time_base;
|
|
|
|
|
|
|
|
if (ist->last_frame_pts != AV_NOPTS_VALUE)
|
|
|
|
ist->last_frame_pts = av_rescale_q(ist->last_frame_pts,
|
|
|
|
ist->last_frame_tb, tb_new);
|
|
|
|
ist->last_frame_duration_est = av_rescale_q(ist->last_frame_duration_est,
|
|
|
|
ist->last_frame_tb, tb_new);
|
|
|
|
|
|
|
|
ist->last_frame_tb = tb_new;
|
|
|
|
ist->last_frame_sample_rate = frame->sample_rate;
|
|
|
|
|
|
|
|
finish:
|
|
|
|
return ist->last_frame_tb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_ts_process(InputStream *ist, AVFrame *frame)
|
|
|
|
{
|
|
|
|
AVRational tb_filter = (AVRational){1, frame->sample_rate};
|
|
|
|
AVRational tb;
|
|
|
|
int64_t pts_pred;
|
|
|
|
|
|
|
|
// on samplerate change, choose a new internal timebase for timestamp
|
|
|
|
// generation that can represent timestamps from all the samplerates
|
|
|
|
// seen so far
|
|
|
|
tb = audio_samplerate_update(ist, frame);
|
|
|
|
pts_pred = ist->last_frame_pts == AV_NOPTS_VALUE ? 0 :
|
|
|
|
ist->last_frame_pts + ist->last_frame_duration_est;
|
|
|
|
|
|
|
|
if (frame->pts == AV_NOPTS_VALUE) {
|
|
|
|
frame->pts = pts_pred;
|
|
|
|
frame->time_base = tb;
|
|
|
|
} else if (ist->last_frame_pts != AV_NOPTS_VALUE &&
|
|
|
|
frame->pts > av_rescale_q_rnd(pts_pred, tb, frame->time_base,
|
|
|
|
AV_ROUND_UP)) {
|
|
|
|
// there was a gap in timestamps, reset conversion state
|
|
|
|
ist->filter_in_rescale_delta_last = AV_NOPTS_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame->pts = av_rescale_delta(frame->time_base, frame->pts,
|
|
|
|
tb, frame->nb_samples,
|
|
|
|
&ist->filter_in_rescale_delta_last, tb);
|
|
|
|
|
|
|
|
ist->last_frame_pts = frame->pts;
|
|
|
|
ist->last_frame_duration_est = av_rescale_q(frame->nb_samples,
|
|
|
|
tb_filter, tb);
|
|
|
|
|
|
|
|
// finally convert to filtering timebase
|
|
|
|
frame->pts = av_rescale_q(frame->pts, tb, tb_filter);
|
|
|
|
frame->duration = frame->nb_samples;
|
|
|
|
frame->time_base = tb_filter;
|
|
|
|
}
|
|
|
|
|
2023-04-26 10:05:26 +02:00
|
|
|
static int decode_audio(InputStream *ist, const AVPacket *pkt, int *got_output,
|
2017-03-22 17:02:51 +02:00
|
|
|
int *decode_failed)
|
2012-06-06 16:56:34 +03:00
|
|
|
{
|
2021-11-15 03:06:58 +02:00
|
|
|
AVFrame *decoded_frame = ist->decoded_frame;
|
2014-06-01 15:35:53 +03:00
|
|
|
AVCodecContext *avctx = ist->dec_ctx;
|
2016-05-27 12:14:33 +02:00
|
|
|
int ret, err = 0;
|
2011-11-21 16:39:22 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
update_benchmark(NULL);
|
2023-01-27 13:19:16 +02:00
|
|
|
ret = decode(ist, avctx, decoded_frame, got_output, pkt);
|
2012-08-09 01:26:38 +03:00
|
|
|
update_benchmark("decode_audio %d.%d", ist->file_index, ist->st->index);
|
2017-03-22 17:02:51 +02:00
|
|
|
if (ret < 0)
|
|
|
|
*decode_failed = 1;
|
2012-08-09 16:14:57 +03:00
|
|
|
|
2016-10-01 17:22:15 +02:00
|
|
|
if (ret != AVERROR_EOF)
|
|
|
|
check_decode_result(ist, got_output, ret);
|
2015-06-30 21:06:40 +02:00
|
|
|
|
2015-06-27 19:21:25 +02:00
|
|
|
if (!*got_output || ret < 0)
|
2011-11-21 23:30:23 +03:00
|
|
|
return ret;
|
2012-06-06 16:56:34 +03:00
|
|
|
|
2014-03-17 12:58:05 +03:00
|
|
|
ist->samples_decoded += decoded_frame->nb_samples;
|
|
|
|
ist->frames_decoded++;
|
|
|
|
|
2023-04-24 12:28:13 +02:00
|
|
|
audio_ts_process(ist, decoded_frame);
|
2023-04-22 16:42:12 +02:00
|
|
|
|
2015-10-14 14:28:20 +02:00
|
|
|
ist->nb_samples = decoded_frame->nb_samples;
|
2016-10-24 23:01:23 +02:00
|
|
|
err = send_frame_to_filters(ist, decoded_frame);
|
2012-05-05 19:22:46 +03:00
|
|
|
|
2013-01-21 23:10:18 +03:00
|
|
|
av_frame_unref(decoded_frame);
|
|
|
|
return err < 0 ? err : ret;
|
2012-06-02 08:26:41 +03:00
|
|
|
}
|
|
|
|
|
2023-04-09 09:55:55 +02:00
|
|
|
static int64_t video_duration_estimate(const InputStream *ist, const AVFrame *frame)
|
|
|
|
{
|
|
|
|
const InputFile *ifile = input_files[ist->file_index];
|
|
|
|
const int container_nots = !!(ifile->ctx->iformat->flags & AVFMT_NOTIMESTAMPS);
|
|
|
|
int64_t codec_duration = 0;
|
|
|
|
|
|
|
|
// XXX lavf currently makes up frame durations when they are not provided by
|
|
|
|
// the container. As there is no way to reliably distinguish real container
|
|
|
|
// durations from the fake made-up ones, we use heuristics based on whether
|
|
|
|
// the container has timestamps. Eventually lavf should stop making up
|
|
|
|
// durations, then this should be simplified.
|
|
|
|
|
|
|
|
// prefer frame duration for containers with timestamps
|
|
|
|
if (frame->duration > 0 && !container_nots)
|
|
|
|
return frame->duration;
|
|
|
|
|
|
|
|
if (ist->dec_ctx->framerate.den && ist->dec_ctx->framerate.num) {
|
|
|
|
int ticks = frame->repeat_pict >= 0 ?
|
|
|
|
frame->repeat_pict + 1 :
|
|
|
|
ist->dec_ctx->ticks_per_frame;
|
|
|
|
codec_duration = av_rescale_q(ticks, av_inv_q(ist->dec_ctx->framerate),
|
|
|
|
ist->st->time_base);
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefer codec-layer duration for containers without timestamps
|
|
|
|
if (codec_duration > 0 && container_nots)
|
|
|
|
return codec_duration;
|
|
|
|
|
|
|
|
// when timestamps are available, repeat last frame's actual duration
|
|
|
|
// (i.e. pts difference between this and last frame)
|
|
|
|
if (frame->pts != AV_NOPTS_VALUE && ist->last_frame_pts != AV_NOPTS_VALUE &&
|
|
|
|
frame->pts > ist->last_frame_pts)
|
|
|
|
return frame->pts - ist->last_frame_pts;
|
|
|
|
|
|
|
|
// try frame/codec duration
|
|
|
|
if (frame->duration > 0)
|
|
|
|
return frame->duration;
|
|
|
|
if (codec_duration > 0)
|
|
|
|
return codec_duration;
|
|
|
|
|
|
|
|
// try average framerate
|
|
|
|
if (ist->st->avg_frame_rate.num && ist->st->avg_frame_rate.den) {
|
|
|
|
int64_t d = av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate),
|
|
|
|
ist->st->time_base);
|
|
|
|
if (d > 0)
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
// last resort is last frame's estimated duration, and 1
|
|
|
|
return FFMAX(ist->last_frame_duration_est, 1);
|
|
|
|
}
|
|
|
|
|
2023-04-26 10:05:26 +02:00
|
|
|
static int decode_video(InputStream *ist, const AVPacket *pkt, int *got_output,
|
|
|
|
int64_t *duration_pts, int eof, int *decode_failed)
|
2012-06-02 08:26:41 +03:00
|
|
|
{
|
2021-11-15 03:06:58 +02:00
|
|
|
AVFrame *decoded_frame = ist->decoded_frame;
|
2023-04-09 09:55:55 +02:00
|
|
|
int ret = 0, err = 0;
|
2012-08-09 01:26:38 +03:00
|
|
|
int64_t best_effort_timestamp;
|
2016-10-01 17:22:15 +02:00
|
|
|
|
|
|
|
// With fate-indeo3-2, we're getting 0-sized packets before EOF for some
|
|
|
|
// reason. This seems like a semi-critical bug. Don't trigger EOF, and
|
|
|
|
// skip the packet.
|
|
|
|
if (!eof && pkt && pkt->size == 0)
|
|
|
|
return 0;
|
2012-06-02 08:26:41 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
update_benchmark(NULL);
|
2023-01-27 13:19:16 +02:00
|
|
|
ret = decode(ist, ist->dec_ctx, decoded_frame, got_output, pkt);
|
2012-08-09 01:26:38 +03:00
|
|
|
update_benchmark("decode_video %d.%d", ist->file_index, ist->st->index);
|
2017-03-22 17:02:51 +02:00
|
|
|
if (ret < 0)
|
|
|
|
*decode_failed = 1;
|
2014-06-03 15:24:37 +03:00
|
|
|
|
|
|
|
// The following line may be required in some cases where there is no parser
|
|
|
|
// or the parser does not has_b_frames correctly
|
2022-08-13 17:03:39 +02:00
|
|
|
if (ist->par->video_delay < ist->dec_ctx->has_b_frames) {
|
2014-06-15 00:23:42 +03:00
|
|
|
if (ist->dec_ctx->codec_id == AV_CODEC_ID_H264) {
|
2022-08-13 17:03:39 +02:00
|
|
|
ist->par->video_delay = ist->dec_ctx->has_b_frames;
|
2014-06-15 00:23:42 +03:00
|
|
|
} else
|
2015-08-08 10:41:32 +02:00
|
|
|
av_log(ist->dec_ctx, AV_LOG_WARNING,
|
2016-09-01 16:48:45 +02:00
|
|
|
"video_delay is larger in decoder than demuxer %d > %d.\n"
|
2015-08-08 10:41:32 +02:00
|
|
|
"If you want to help, upload a sample "
|
2020-05-10 21:01:35 +02:00
|
|
|
"of this file to https://streams.videolan.org/upload/ "
|
2017-02-02 10:45:15 +02:00
|
|
|
"and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n",
|
2015-08-08 10:41:32 +02:00
|
|
|
ist->dec_ctx->has_b_frames,
|
2022-08-13 17:03:39 +02:00
|
|
|
ist->par->video_delay);
|
2014-06-03 16:09:42 +03:00
|
|
|
}
|
2013-04-10 02:10:06 +03:00
|
|
|
|
2016-10-01 17:22:15 +02:00
|
|
|
if (ret != AVERROR_EOF)
|
|
|
|
check_decode_result(ist, got_output, ret);
|
2015-06-30 21:06:40 +02:00
|
|
|
|
2014-11-27 15:16:31 +02:00
|
|
|
if (*got_output && ret >= 0) {
|
|
|
|
if (ist->dec_ctx->width != decoded_frame->width ||
|
|
|
|
ist->dec_ctx->height != decoded_frame->height ||
|
|
|
|
ist->dec_ctx->pix_fmt != decoded_frame->format) {
|
|
|
|
av_log(NULL, AV_LOG_DEBUG, "Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n",
|
|
|
|
decoded_frame->width,
|
|
|
|
decoded_frame->height,
|
|
|
|
decoded_frame->format,
|
|
|
|
ist->dec_ctx->width,
|
|
|
|
ist->dec_ctx->height,
|
|
|
|
ist->dec_ctx->pix_fmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-27 19:21:25 +02:00
|
|
|
if (!*got_output || ret < 0)
|
2011-11-21 23:30:23 +03:00
|
|
|
return ret;
|
2012-06-02 08:26:41 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
if(ist->top_field_first>=0)
|
2023-04-12 16:42:35 +02:00
|
|
|
decoded_frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
|
2012-06-02 08:26:41 +03:00
|
|
|
|
2014-03-17 12:58:05 +03:00
|
|
|
ist->frames_decoded++;
|
|
|
|
|
2013-11-03 00:06:36 +03:00
|
|
|
if (ist->hwaccel_retrieve_data && decoded_frame->format == ist->hwaccel_pix_fmt) {
|
2014-06-01 15:35:53 +03:00
|
|
|
err = ist->hwaccel_retrieve_data(ist->dec_ctx, decoded_frame);
|
2013-11-03 00:06:36 +03:00
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-11-23 16:26:27 +03:00
|
|
|
|
2017-04-22 10:58:03 +02:00
|
|
|
best_effort_timestamp= decoded_frame->best_effort_timestamp;
|
2022-07-11 10:31:47 +02:00
|
|
|
*duration_pts = decoded_frame->duration;
|
2016-10-01 17:22:15 +02:00
|
|
|
|
2017-03-12 14:08:04 +02:00
|
|
|
if (ist->framerate.num)
|
|
|
|
best_effort_timestamp = ist->cfr_next_pts++;
|
|
|
|
|
2023-04-09 09:55:55 +02:00
|
|
|
// no timestamp available - extrapolate from previous frame duration
|
2023-04-08 16:22:41 +02:00
|
|
|
if (best_effort_timestamp == AV_NOPTS_VALUE)
|
2023-04-25 23:11:25 +02:00
|
|
|
best_effort_timestamp = ist->last_frame_pts == AV_NOPTS_VALUE ? 0 :
|
|
|
|
ist->last_frame_pts + ist->last_frame_duration_est;
|
2023-04-08 16:22:41 +02:00
|
|
|
|
2016-02-18 02:07:33 +02:00
|
|
|
if(best_effort_timestamp != AV_NOPTS_VALUE) {
|
2023-04-26 00:11:59 +02:00
|
|
|
decoded_frame->pts = best_effort_timestamp;
|
2016-02-18 02:07:33 +02:00
|
|
|
}
|
2012-06-02 08:26:41 +03:00
|
|
|
|
2023-04-09 09:55:55 +02:00
|
|
|
// update timestamp history
|
|
|
|
ist->last_frame_duration_est = video_duration_estimate(ist, decoded_frame);
|
|
|
|
ist->last_frame_pts = decoded_frame->pts;
|
2023-04-24 12:28:13 +02:00
|
|
|
ist->last_frame_tb = decoded_frame->time_base;
|
2023-04-09 09:55:55 +02:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
if (debug_ts) {
|
2023-04-07 11:03:37 +02:00
|
|
|
av_log(ist, AV_LOG_INFO,
|
|
|
|
"decoder -> pts:%s pts_time:%s "
|
|
|
|
"pkt_dts:%s pkt_dts_time:%s "
|
|
|
|
"best_effort_ts:%"PRId64" best_effort_ts_time:%s "
|
|
|
|
"duration:%s duration_time:%s "
|
|
|
|
"keyframe:%d frame_type:%d time_base:%d/%d\n",
|
|
|
|
av_ts2str(decoded_frame->pts),
|
2014-01-25 16:12:15 +03:00
|
|
|
av_ts2timestr(decoded_frame->pts, &ist->st->time_base),
|
2023-04-07 11:03:37 +02:00
|
|
|
av_ts2str(decoded_frame->pkt_dts),
|
|
|
|
av_ts2timestr(decoded_frame->pkt_dts, &ist->st->time_base),
|
2014-01-25 16:12:15 +03:00
|
|
|
best_effort_timestamp,
|
|
|
|
av_ts2timestr(best_effort_timestamp, &ist->st->time_base),
|
2023-04-07 11:03:37 +02:00
|
|
|
av_ts2str(decoded_frame->duration),
|
|
|
|
av_ts2timestr(decoded_frame->duration, &ist->st->time_base),
|
2023-04-13 00:54:10 +02:00
|
|
|
!!(decoded_frame->flags & AV_FRAME_FLAG_KEY), decoded_frame->pict_type,
|
2014-01-25 16:12:15 +03:00
|
|
|
ist->st->time_base.num, ist->st->time_base.den);
|
2012-06-02 08:26:41 +03:00
|
|
|
}
|
|
|
|
|
2012-03-22 11:37:33 +03:00
|
|
|
if (ist->st->sample_aspect_ratio.num)
|
|
|
|
decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
|
2012-06-02 08:26:41 +03:00
|
|
|
|
2016-10-24 23:01:23 +02:00
|
|
|
err = send_frame_to_filters(ist, decoded_frame);
|
2012-06-02 08:26:41 +03:00
|
|
|
|
2013-11-03 00:06:36 +03:00
|
|
|
fail:
|
2013-01-21 23:10:18 +03:00
|
|
|
av_frame_unref(decoded_frame);
|
|
|
|
return err < 0 ? err : ret;
|
2012-06-02 08:26:41 +03:00
|
|
|
}
|
|
|
|
|
2021-12-13 18:30:37 +02:00
|
|
|
static int process_subtitle(InputStream *ist, AVSubtitle *subtitle, int *got_output)
|
2011-08-30 17:03:51 +03:00
|
|
|
{
|
2021-12-13 18:30:37 +02:00
|
|
|
int ret = 0;
|
2017-03-02 17:01:01 +02:00
|
|
|
int free_sub = 1;
|
2001-09-25 02:22:25 +03:00
|
|
|
|
2012-08-09 12:44:17 +03:00
|
|
|
if (ist->fix_sub_duration) {
|
2013-11-23 19:48:49 +03:00
|
|
|
int end = 1;
|
2012-08-09 12:44:17 +03:00
|
|
|
if (ist->prev_sub.got_output) {
|
2021-12-13 18:30:37 +02:00
|
|
|
end = av_rescale(subtitle->pts - ist->prev_sub.subtitle.pts,
|
2013-11-23 19:48:49 +03:00
|
|
|
1000, AV_TIME_BASE);
|
2012-08-09 12:44:17 +03:00
|
|
|
if (end < ist->prev_sub.subtitle.end_display_time) {
|
2022-06-20 09:53:58 +02:00
|
|
|
av_log(NULL, AV_LOG_DEBUG,
|
2017-03-27 21:31:46 +02:00
|
|
|
"Subtitle duration reduced from %"PRId32" to %d%s\n",
|
2013-11-23 19:48:49 +03:00
|
|
|
ist->prev_sub.subtitle.end_display_time, end,
|
|
|
|
end <= 0 ? ", dropping it" : "");
|
2012-08-09 12:44:17 +03:00
|
|
|
ist->prev_sub.subtitle.end_display_time = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FFSWAP(int, *got_output, ist->prev_sub.got_output);
|
|
|
|
FFSWAP(int, ret, ist->prev_sub.ret);
|
2021-12-13 18:30:37 +02:00
|
|
|
FFSWAP(AVSubtitle, *subtitle, ist->prev_sub.subtitle);
|
2013-11-23 19:48:49 +03:00
|
|
|
if (end <= 0)
|
|
|
|
goto out;
|
2012-08-09 12:44:17 +03:00
|
|
|
}
|
|
|
|
|
2013-11-23 19:25:46 +03:00
|
|
|
if (!*got_output)
|
|
|
|
return ret;
|
|
|
|
|
2017-03-02 17:01:01 +02:00
|
|
|
if (ist->sub2video.frame) {
|
2021-12-13 18:30:37 +02:00
|
|
|
sub2video_update(ist, INT64_MIN, subtitle);
|
2017-03-02 17:01:01 +02:00
|
|
|
} else if (ist->nb_filters) {
|
|
|
|
if (!ist->sub2video.sub_queue)
|
2022-01-10 12:04:36 +02:00
|
|
|
ist->sub2video.sub_queue = av_fifo_alloc2(8, sizeof(AVSubtitle), AV_FIFO_FLAG_AUTO_GROW);
|
2017-03-02 17:01:01 +02:00
|
|
|
if (!ist->sub2video.sub_queue)
|
2022-08-27 15:41:16 +02:00
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
2022-01-10 12:04:36 +02:00
|
|
|
|
2021-12-13 18:30:37 +02:00
|
|
|
ret = av_fifo_write(ist->sub2video.sub_queue, subtitle, 1);
|
2022-01-10 12:04:36 +02:00
|
|
|
if (ret < 0)
|
|
|
|
exit_program(1);
|
2017-03-02 17:01:01 +02:00
|
|
|
free_sub = 0;
|
|
|
|
}
|
2012-09-09 16:53:21 +03:00
|
|
|
|
2021-12-13 18:30:37 +02:00
|
|
|
if (!subtitle->num_rects)
|
2013-11-23 19:27:22 +03:00
|
|
|
goto out;
|
2012-08-09 12:44:17 +03: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 || ost->type != AVMEDIA_TYPE_SUBTITLE)
|
2009-01-22 14:04:01 +02:00
|
|
|
continue;
|
2012-04-02 21:13:29 +03:00
|
|
|
|
2023-03-25 06:37:42 +02:00
|
|
|
enc_subtitle(output_files[ost->file_index], ost, subtitle);
|
2011-11-21 16:39:22 +03:00
|
|
|
}
|
2004-01-22 21:48:28 +02:00
|
|
|
|
2013-11-23 19:27:22 +03:00
|
|
|
out:
|
2017-03-02 17:01:01 +02:00
|
|
|
if (free_sub)
|
2021-12-13 18:30:37 +02:00
|
|
|
avsubtitle_free(subtitle);
|
2011-11-21 23:30:23 +03:00
|
|
|
return ret;
|
2011-11-21 16:39:22 +03:00
|
|
|
}
|
2009-03-03 21:50:04 +02:00
|
|
|
|
2022-07-22 12:57:54 +02:00
|
|
|
static int copy_av_subtitle(AVSubtitle *dst, AVSubtitle *src)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts)
|
|
|
|
{
|
|
|
|
int ret = AVERROR_BUG;
|
|
|
|
int got_output = 1;
|
|
|
|
AVSubtitle *prev_subtitle = &ist->prev_sub.subtitle;
|
|
|
|
AVSubtitle subtitle;
|
|
|
|
|
|
|
|
if (!ist->fix_sub_duration || !prev_subtitle->num_rects ||
|
|
|
|
signal_pts <= prev_subtitle->pts)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((ret = copy_av_subtitle(&subtitle, prev_subtitle)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
subtitle.pts = signal_pts;
|
|
|
|
|
|
|
|
return process_subtitle(ist, &subtitle, &got_output);
|
|
|
|
}
|
|
|
|
|
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-01-27 16:05:58 +02:00
|
|
|
static int transcode_subtitles(InputStream *ist, const AVPacket *pkt,
|
|
|
|
int *got_output, int *decode_failed)
|
2021-12-13 18:30:37 +02:00
|
|
|
{
|
|
|
|
AVSubtitle subtitle;
|
|
|
|
int ret = avcodec_decode_subtitle2(ist->dec_ctx,
|
|
|
|
&subtitle, got_output, pkt);
|
|
|
|
|
|
|
|
check_decode_result(NULL, got_output, ret);
|
|
|
|
|
|
|
|
if (ret < 0 || !*got_output) {
|
|
|
|
*decode_failed = 1;
|
|
|
|
if (!pkt->size)
|
|
|
|
sub2video_flush(ist);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-05 09:50:57 +02:00
|
|
|
ist->frames_decoded++;
|
|
|
|
|
2021-12-13 18:30:37 +02:00
|
|
|
return process_subtitle(ist, &subtitle, got_output);
|
|
|
|
}
|
|
|
|
|
2015-06-27 19:21:25 +02:00
|
|
|
static int send_filter_eof(InputStream *ist)
|
|
|
|
{
|
2016-06-27 19:03:42 +02:00
|
|
|
int i, ret;
|
2017-04-06 10:40:12 +02:00
|
|
|
|
2015-06-27 19:21:25 +02:00
|
|
|
for (i = 0; i < ist->nb_filters; i++) {
|
2023-04-26 00:09:50 +02:00
|
|
|
int64_t end_pts = ist->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
|
|
|
|
ist->last_frame_pts + ist->last_frame_duration_est;
|
|
|
|
ret = ifilter_send_eof(ist->filters[i], end_pts, ist->last_frame_tb);
|
2016-06-27 19:03:42 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-06-27 19:21:25 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
/* pkt = NULL means EOF (needed to flush decoder buffers) */
|
2015-10-14 14:28:20 +02:00
|
|
|
static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
|
2011-07-27 21:56:59 +03:00
|
|
|
{
|
2023-04-03 13:47:12 +02:00
|
|
|
InputFile *f = input_files[ist->file_index];
|
2022-08-13 17:03:39 +02:00
|
|
|
const AVCodecParameters *par = ist->par;
|
2022-10-14 10:57:19 +02:00
|
|
|
int ret = 0;
|
2016-10-01 17:22:15 +02:00
|
|
|
int repeating = 0;
|
|
|
|
int eof_reached = 0;
|
2023-04-03 13:47:12 +02:00
|
|
|
int duration_exceeded;
|
2001-07-22 17:37:44 +03:00
|
|
|
|
2021-10-08 11:24:09 +02:00
|
|
|
AVPacket *avpkt = ist->pkt;
|
2021-01-30 00:09:18 +02:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
if (!ist->saw_first_ts) {
|
2021-05-31 22:15:56 +02:00
|
|
|
ist->first_dts =
|
2014-06-01 15:53:34 +03:00
|
|
|
ist->dts = ist->st->avg_frame_rate.num ? - ist->dec_ctx->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
|
2023-04-26 10:51:38 +02:00
|
|
|
if (pkt && pkt->pts != AV_NOPTS_VALUE) {
|
2021-05-31 22:15:56 +02:00
|
|
|
ist->first_dts =
|
2023-04-17 13:41:37 +02:00
|
|
|
ist->dts += av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
|
2012-07-28 17:55:12 +03:00
|
|
|
}
|
2012-08-09 01:26:38 +03:00
|
|
|
ist->saw_first_ts = 1;
|
|
|
|
}
|
2012-07-28 17:55:12 +03:00
|
|
|
|
2012-02-03 17:32:51 +03:00
|
|
|
if (ist->next_dts == AV_NOPTS_VALUE)
|
2012-08-09 01:26:38 +03:00
|
|
|
ist->next_dts = ist->dts;
|
2007-08-03 22:21:50 +03:00
|
|
|
|
2021-01-30 00:09:18 +02:00
|
|
|
if (pkt) {
|
|
|
|
av_packet_unref(avpkt);
|
|
|
|
ret = av_packet_ref(avpkt, pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-07-27 21:56:59 +03:00
|
|
|
}
|
2008-07-14 01:55:52 +03:00
|
|
|
|
2016-10-01 17:22:15 +02:00
|
|
|
if (pkt && pkt->dts != AV_NOPTS_VALUE) {
|
2023-04-17 13:41:37 +02:00
|
|
|
ist->next_dts = ist->dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
2012-01-20 19:06:26 +03:00
|
|
|
|
2011-12-29 23:28:29 +03:00
|
|
|
// while we have more to decode or while the decoder did output something on EOF
|
2016-10-01 17:22:15 +02:00
|
|
|
while (ist->decoding_needed) {
|
2017-04-03 14:40:44 +02:00
|
|
|
int64_t duration_pts = 0;
|
2016-10-01 17:22:15 +02:00
|
|
|
int got_output = 0;
|
2017-03-22 17:02:51 +02:00
|
|
|
int decode_failed = 0;
|
2004-06-12 06:41:36 +03:00
|
|
|
|
2022-06-20 10:16:47 +02:00
|
|
|
switch (par->codec_type) {
|
2011-11-21 17:37:40 +03:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2021-01-30 00:09:18 +02:00
|
|
|
ret = decode_audio (ist, repeating ? NULL : avpkt, &got_output,
|
2017-03-22 17:02:51 +02:00
|
|
|
&decode_failed);
|
2021-01-30 00:09:18 +02:00
|
|
|
av_packet_unref(avpkt);
|
2011-11-21 17:37:40 +03:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2021-01-30 00:09:18 +02:00
|
|
|
ret = decode_video (ist, repeating ? NULL : avpkt, &got_output, &duration_pts, !pkt,
|
2017-03-22 17:02:51 +02:00
|
|
|
&decode_failed);
|
2003-12-15 16:43:44 +02:00
|
|
|
|
2021-01-30 00:09:18 +02:00
|
|
|
av_packet_unref(avpkt);
|
2011-11-21 17:37:40 +03:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_SUBTITLE:
|
2016-10-01 17:22:15 +02:00
|
|
|
if (repeating)
|
|
|
|
break;
|
2021-01-30 00:09:18 +02:00
|
|
|
ret = transcode_subtitles(ist, avpkt, &got_output, &decode_failed);
|
2016-10-01 17:22:15 +02:00
|
|
|
if (!pkt && ret >= 0)
|
|
|
|
ret = AVERROR_EOF;
|
2021-01-30 00:09:18 +02:00
|
|
|
av_packet_unref(avpkt);
|
2011-11-21 17:37:40 +03:00
|
|
|
break;
|
2011-11-21 16:06:25 +03:00
|
|
|
default:
|
|
|
|
return -1;
|
2003-12-15 16:43:44 +02:00
|
|
|
}
|
2001-07-22 17:37:44 +03:00
|
|
|
|
2016-10-01 17:22:15 +02:00
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
eof_reached = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-06-27 20:34:42 +02:00
|
|
|
if (ret < 0) {
|
2017-03-22 17:02:51 +02:00
|
|
|
if (decode_failed) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d: %s\n",
|
|
|
|
ist->file_index, ist->st->index, av_err2str(ret));
|
|
|
|
} else {
|
|
|
|
av_log(NULL, AV_LOG_FATAL, "Error while processing the decoded "
|
|
|
|
"data for stream #%d:%d\n", ist->file_index, ist->st->index);
|
|
|
|
}
|
|
|
|
if (!decode_failed || exit_on_error)
|
2015-06-27 20:34:42 +02:00
|
|
|
exit_program(1);
|
|
|
|
break;
|
|
|
|
}
|
2004-03-06 00:51:22 +02:00
|
|
|
|
2016-05-27 12:14:33 +02:00
|
|
|
if (got_output)
|
|
|
|
ist->got_output = 1;
|
|
|
|
|
2016-10-01 17:22:15 +02:00
|
|
|
if (!got_output)
|
|
|
|
break;
|
2004-04-15 19:34:38 +03:00
|
|
|
|
2016-10-01 17:22:15 +02:00
|
|
|
// During draining, we might get multiple output frames in this loop.
|
|
|
|
// ffmpeg.c does not drain the filter chain on configuration changes,
|
|
|
|
// which means if we send multiple frames at once to the filters, and
|
|
|
|
// one of those frames changes configuration, the buffered frames will
|
|
|
|
// be lost. This can upset certain FATE tests.
|
|
|
|
// Decode only 1 frame per call on EOF to appease these FATE tests.
|
|
|
|
// The ideal solution would be to rewrite decoding to use the new
|
|
|
|
// decoding API in a better way.
|
|
|
|
if (!pkt)
|
2014-06-23 00:54:51 +03:00
|
|
|
break;
|
2016-10-01 17:22:15 +02:00
|
|
|
|
|
|
|
repeating = 1;
|
2001-07-22 17:37:44 +03:00
|
|
|
}
|
|
|
|
|
2015-06-27 19:21:25 +02:00
|
|
|
/* after flushing, send an EOF on all the filter inputs attached to the stream */
|
2015-10-14 14:28:20 +02:00
|
|
|
/* except when looping we need to flush but not to send an EOF */
|
2016-10-01 17:22:15 +02:00
|
|
|
if (!pkt && ist->decoding_needed && eof_reached && !no_eof) {
|
2015-06-27 19:21:25 +02:00
|
|
|
int ret = send_filter_eof(ist);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");
|
|
|
|
exit_program(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 10:51:38 +02:00
|
|
|
if (pkt) {
|
2012-08-09 01:26:38 +03:00
|
|
|
ist->dts = ist->next_dts;
|
2022-06-20 10:16:47 +02:00
|
|
|
switch (par->codec_type) {
|
2011-11-21 16:05:38 +03:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2018-06-13 16:50:39 +02:00
|
|
|
av_assert1(pkt->duration >= 0);
|
2022-06-20 10:16:47 +02:00
|
|
|
if (par->sample_rate) {
|
|
|
|
ist->next_dts += ((int64_t)AV_TIME_BASE * par->frame_size) /
|
|
|
|
par->sample_rate;
|
2018-05-01 22:44:07 +02:00
|
|
|
} else {
|
2023-04-17 13:41:37 +02:00
|
|
|
ist->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
|
2018-05-01 22:44:07 +02:00
|
|
|
}
|
2011-11-21 16:05:38 +03:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2013-04-02 19:08:12 +03:00
|
|
|
if (ist->framerate.num) {
|
2013-04-04 00:58:20 +03:00
|
|
|
// TODO: Remove work-around for c99-to-c89 issue 7
|
|
|
|
AVRational time_base_q = AV_TIME_BASE_Q;
|
|
|
|
int64_t next_dts = av_rescale_q(ist->next_dts, time_base_q, av_inv_q(ist->framerate));
|
|
|
|
ist->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
|
2017-11-01 02:48:56 +02:00
|
|
|
} else if (pkt->duration) {
|
2023-04-17 13:41:37 +02:00
|
|
|
ist->next_dts += av_rescale_q(pkt->duration, pkt->time_base, AV_TIME_BASE_Q);
|
2014-10-15 19:47:44 +03:00
|
|
|
} else if(ist->dec_ctx->framerate.num != 0) {
|
2022-08-18 11:13:08 +02:00
|
|
|
int ticks = ist->last_pkt_repeat_pict >= 0 ?
|
|
|
|
ist->last_pkt_repeat_pict + 1 :
|
|
|
|
ist->dec_ctx->ticks_per_frame;
|
2012-02-03 17:32:51 +03:00
|
|
|
ist->next_dts += ((int64_t)AV_TIME_BASE *
|
2014-10-15 19:47:44 +03:00
|
|
|
ist->dec_ctx->framerate.den * ticks) /
|
|
|
|
ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;
|
2001-07-22 17:37:44 +03:00
|
|
|
}
|
2011-11-21 16:05:38 +03:00
|
|
|
break;
|
2001-07-22 17:37:44 +03:00
|
|
|
}
|
2023-04-26 10:51:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pkt && !ist->decoding_needed)
|
2022-02-14 19:07:01 +02:00
|
|
|
eof_reached = 1;
|
|
|
|
|
2023-04-03 13:47:12 +02:00
|
|
|
duration_exceeded = 0;
|
|
|
|
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-04 09:25:13 +02:00
|
|
|
if (ist->dts >= f->recording_time + start_time)
|
2023-04-03 13:47:12 +02:00
|
|
|
duration_exceeded = 1;
|
|
|
|
}
|
|
|
|
|
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-04-03 13:47:12 +02:00
|
|
|
if (duration_exceeded) {
|
|
|
|
close_output_stream(ost);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-04-13 14:32:57 +02:00
|
|
|
of_streamcopy(ost, pkt, ist->dts);
|
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
|
|
|
}
|
|
|
|
|
2012-04-15 16:03:07 +03:00
|
|
|
static int transcode_init(void)
|
2011-07-27 21:56:59 +03:00
|
|
|
{
|
2022-11-17 10:56:37 +02:00
|
|
|
int ret = 0;
|
2012-03-16 06:07:59 +03:00
|
|
|
|
2011-08-29 10:16:42 +03:00
|
|
|
/* init framerate emulation */
|
2022-11-17 10:56:37 +02:00
|
|
|
for (int i = 0; i < nb_input_files; i++) {
|
2012-04-15 16:28:30 +03:00
|
|
|
InputFile *ifile = input_files[i];
|
2023-05-02 15:27:24 +02:00
|
|
|
if (ifile->readrate)
|
2022-11-17 10:56:37 +02:00
|
|
|
for (int j = 0; j < ifile->nb_streams; j++)
|
2022-11-17 10:49:27 +02:00
|
|
|
ifile->streams[j]->start = av_gettime_relative();
|
2011-12-17 03:36:59 +03:00
|
|
|
}
|
2004-07-21 04:35:32 +03:00
|
|
|
|
2011-08-31 23:24:06 +03:00
|
|
|
/* discard unused programs */
|
2022-11-17 10:56:37 +02:00
|
|
|
for (int i = 0; i < nb_input_files; i++) {
|
2012-04-15 16:28:30 +03:00
|
|
|
InputFile *ifile = input_files[i];
|
2022-11-17 10:56:37 +02:00
|
|
|
for (int j = 0; j < ifile->ctx->nb_programs; j++) {
|
2011-08-31 23:24:06 +03:00
|
|
|
AVProgram *p = ifile->ctx->programs[j];
|
|
|
|
int discard = AVDISCARD_ALL;
|
2007-08-08 17:34:51 +03:00
|
|
|
|
2022-11-17 10:56:37 +02:00
|
|
|
for (int k = 0; k < p->nb_stream_indexes; k++)
|
2022-11-17 10:49:27 +02:00
|
|
|
if (!ifile->streams[p->stream_index[k]]->discard) {
|
2011-08-31 23:24:06 +03:00
|
|
|
discard = AVDISCARD_DEFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p->discard = discard;
|
2010-03-02 02:01:16 +02:00
|
|
|
}
|
2009-10-30 11:57:34 +02:00
|
|
|
}
|
|
|
|
|
2011-07-27 21:56:59 +03:00
|
|
|
/* dump the stream mapping */
|
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",
|
|
|
|
ist->file_index, ist->st->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,
|
|
|
|
ost->ist->st->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
|
|
|
}
|
|
|
|
|
2023-04-11 11:23:21 +02:00
|
|
|
if (ret)
|
2011-08-20 00:45:02 +03:00
|
|
|
return ret;
|
2011-08-30 05:19:26 +03:00
|
|
|
|
2017-03-31 17:27:20 +02:00
|
|
|
atomic_store(&transcode_init_done, 1);
|
2014-04-10 23:23:54 +03:00
|
|
|
|
2011-08-20 00:45:02 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2011-09-05 23:10:26 +03:00
|
|
|
|
2012-08-24 14:31:50 +03:00
|
|
|
/* Return 1 if there remain streams where more output is wanted, 0 otherwise. */
|
2012-06-01 11:44:11 +03:00
|
|
|
static int need_output(void)
|
|
|
|
{
|
2022-10-14 10:57:19 +02:00
|
|
|
for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
|
2022-03-31 17:33:48 +02:00
|
|
|
if (ost->finished)
|
2012-06-01 11:44:11 +03:00
|
|
|
continue;
|
2011-09-10 21:52:48 +03:00
|
|
|
|
2012-06-01 11:44:11 +03:00
|
|
|
return 1;
|
2011-09-12 00:35:35 +03:00
|
|
|
}
|
2006-07-06 18:04:46 +03:00
|
|
|
|
2012-06-01 11:44:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2012-03-29 16:45:50 +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
|
|
|
*
|
2012-08-17 14:50:16 +03:00
|
|
|
* @return selected output stream, or NULL if none available
|
2012-08-09 01:26:38 +03:00
|
|
|
*/
|
2012-08-17 14:50:16 +03:00
|
|
|
static OutputStream *choose_output(void)
|
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
|
|
|
if (ost->last_mux_dts == AV_NOPTS_VALUE)
|
2023-01-28 11:06:20 +02:00
|
|
|
av_log(ost, AV_LOG_DEBUG,
|
|
|
|
"cur_dts is invalid [init:%d i_done:%d finish:%d] (this is harmless if it occurs once at the start per stream)\n",
|
|
|
|
ost->initialized, ost->inputs_done, ost->finished);
|
2022-06-10 14:38:32 +02:00
|
|
|
}
|
2015-11-11 18:19:12 +02:00
|
|
|
|
2023-03-27 07:15:00 +02:00
|
|
|
if (!ost->initialized && !ost->inputs_done && !ost->finished)
|
2021-05-06 22:31:44 +02:00
|
|
|
return ost->unavailable ? NULL : ost;
|
2016-05-27 12:14:33 +02:00
|
|
|
|
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;
|
2014-10-22 18:08:00 +03:00
|
|
|
ost_min = ost->unavailable ? NULL : ost;
|
2012-01-17 17:25:14 +03:00
|
|
|
}
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
2012-08-17 14:50:16 +03:00
|
|
|
return ost_min;
|
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 int got_eagain(void)
|
|
|
|
{
|
2022-10-14 10:57:19 +02:00
|
|
|
for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost))
|
|
|
|
if (ost->unavailable)
|
2012-08-04 13:04:02 +03:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
int ret;
|
|
|
|
|
2023-03-20 17:50:19 +02:00
|
|
|
if (ist->discard)
|
2022-03-22 11:41:55 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = process_input_packet(ist, NULL, 1);
|
|
|
|
} while (ret > 0);
|
|
|
|
|
|
|
|
if (ist->decoding_needed) {
|
|
|
|
/* report last frame duration to the demuxer thread */
|
2022-08-13 17:03:39 +02:00
|
|
|
if (ist->par->codec_type == AVMEDIA_TYPE_AUDIO) {
|
2022-03-22 11:41:55 +02:00
|
|
|
LastFrameDuration dur;
|
|
|
|
|
|
|
|
dur.stream_idx = i;
|
|
|
|
dur.duration = av_rescale_q(ist->nb_samples,
|
|
|
|
(AVRational){ 1, ist->dec_ctx->sample_rate},
|
|
|
|
ist->st->time_base);
|
|
|
|
|
|
|
|
av_thread_message_queue_send(ifile->audio_duration_queue, &dur, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
avcodec_flush_buffers(ist->dec_ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 17:37:12 +02:00
|
|
|
static void ts_discontinuity_detect(InputFile *ifile, InputStream *ist,
|
|
|
|
AVPacket *pkt)
|
2022-08-10 16:33:43 +02:00
|
|
|
{
|
|
|
|
const int fmt_is_discont = ifile->ctx->iformat->flags & AVFMT_TS_DISCONT;
|
|
|
|
int disable_discontinuity_correction = copy_ts;
|
2023-04-17 13:41:37 +02:00
|
|
|
int64_t pkt_dts = av_rescale_q_rnd(pkt->dts, pkt->time_base, AV_TIME_BASE_Q,
|
2022-08-10 16:33:43 +02:00
|
|
|
AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
|
|
|
|
|
|
|
|
if (copy_ts && ist->next_dts != AV_NOPTS_VALUE &&
|
|
|
|
fmt_is_discont && ist->st->pts_wrap_bits < 60) {
|
|
|
|
int64_t wrap_dts = av_rescale_q_rnd(pkt->dts + (1LL<<ist->st->pts_wrap_bits),
|
2023-04-17 13:41:37 +02:00
|
|
|
pkt->time_base, AV_TIME_BASE_Q,
|
2022-08-10 16:33:43 +02:00
|
|
|
AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
|
|
|
|
if (FFABS(wrap_dts - ist->next_dts) < FFABS(pkt_dts - ist->next_dts)/10)
|
|
|
|
disable_discontinuity_correction = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ist->next_dts != AV_NOPTS_VALUE && !disable_discontinuity_correction) {
|
|
|
|
int64_t delta = pkt_dts - ist->next_dts;
|
|
|
|
if (fmt_is_discont) {
|
2022-08-10 17:04:30 +02:00
|
|
|
if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE ||
|
2023-04-15 10:18:24 +02:00
|
|
|
pkt_dts + AV_TIME_BASE/10 < ist->dts) {
|
2022-08-10 17:37:12 +02:00
|
|
|
ifile->ts_offset_discont -= delta;
|
2017-09-13 10:40:15 +02:00
|
|
|
av_log(NULL, AV_LOG_WARNING,
|
2022-08-10 16:33:43 +02:00
|
|
|
"timestamp discontinuity for stream #%d:%d "
|
|
|
|
"(id=%d, type=%s): %"PRId64", new offset= %"PRId64"\n",
|
|
|
|
ist->file_index, ist->st->index, ist->st->id,
|
2022-08-13 17:03:39 +02:00
|
|
|
av_get_media_type_string(ist->par->codec_type),
|
2022-08-10 17:37:12 +02:00
|
|
|
delta, ifile->ts_offset_discont);
|
2023-04-17 13:41:37 +02:00
|
|
|
pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
|
2022-08-10 16:33:43 +02:00
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
2023-04-17 13:41:37 +02:00
|
|
|
pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
|
2022-08-10 16:33:43 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-10 17:04:30 +02:00
|
|
|
if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
|
2022-08-10 16:33:43 +02:00
|
|
|
av_log(NULL, AV_LOG_WARNING, "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n", pkt->dts, ist->next_dts, pkt->stream_index);
|
|
|
|
pkt->dts = AV_NOPTS_VALUE;
|
|
|
|
}
|
|
|
|
if (pkt->pts != AV_NOPTS_VALUE){
|
2023-04-17 13:41:37 +02:00
|
|
|
int64_t pkt_pts = av_rescale_q(pkt->pts, pkt->time_base, AV_TIME_BASE_Q);
|
2022-08-10 16:33:43 +02:00
|
|
|
delta = pkt_pts - ist->next_dts;
|
2022-08-10 17:04:30 +02:00
|
|
|
if (FFABS(delta) > 1LL * dts_error_threshold * AV_TIME_BASE) {
|
2022-08-10 16:33:43 +02:00
|
|
|
av_log(NULL, AV_LOG_WARNING, "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n", pkt->pts, ist->next_dts, pkt->stream_index);
|
|
|
|
pkt->pts = AV_NOPTS_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-10 16:48:03 +02:00
|
|
|
} else if (ist->next_dts == AV_NOPTS_VALUE && !copy_ts &&
|
|
|
|
fmt_is_discont && ifile->last_ts != AV_NOPTS_VALUE) {
|
|
|
|
int64_t delta = pkt_dts - ifile->last_ts;
|
2022-08-10 17:04:30 +02:00
|
|
|
if (FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE) {
|
2022-08-10 17:37:12 +02:00
|
|
|
ifile->ts_offset_discont -= delta;
|
2022-08-10 16:48:03 +02:00
|
|
|
av_log(NULL, AV_LOG_DEBUG,
|
|
|
|
"Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
|
2022-08-10 17:37:12 +02:00
|
|
|
delta, ifile->ts_offset_discont);
|
2023-04-17 13:41:37 +02:00
|
|
|
pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
|
2022-08-10 16:48:03 +02:00
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
2023-04-17 13:41:37 +02:00
|
|
|
pkt->pts -= av_rescale_q(delta, AV_TIME_BASE_Q, pkt->time_base);
|
2022-08-10 16:48:03 +02:00
|
|
|
}
|
2022-08-10 16:33:43 +02:00
|
|
|
}
|
2022-08-10 16:48:03 +02:00
|
|
|
|
2023-04-17 13:41:37 +02:00
|
|
|
ifile->last_ts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
|
2022-08-10 16:33:43 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 17:37:12 +02:00
|
|
|
static void ts_discontinuity_process(InputFile *ifile, InputStream *ist,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int64_t offset = av_rescale_q(ifile->ts_offset_discont, AV_TIME_BASE_Q,
|
2023-04-17 13:41:37 +02:00
|
|
|
pkt->time_base);
|
2022-08-10 17:37:12 +02:00
|
|
|
|
|
|
|
// apply previously-detected timestamp-discontinuity offset
|
|
|
|
// (to all streams, not just audio/video)
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE)
|
|
|
|
pkt->dts += offset;
|
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
|
|
|
pkt->pts += offset;
|
|
|
|
|
|
|
|
// detect timestamp discontinuities for audio/video
|
2022-08-13 17:03:39 +02:00
|
|
|
if ((ist->par->codec_type == AVMEDIA_TYPE_VIDEO ||
|
|
|
|
ist->par->codec_type == AVMEDIA_TYPE_AUDIO) &&
|
2022-08-10 17:37:12 +02:00
|
|
|
pkt->dts != AV_NOPTS_VALUE)
|
|
|
|
ts_discontinuity_detect(ifile, ist, pkt);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
AVFormatContext *is;
|
|
|
|
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
|
|
|
|
|
|
|
is = ifile->ctx;
|
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) {
|
2017-12-30 00:29:27 +02:00
|
|
|
print_error(is->url, ret);
|
2012-08-09 19:04:17 +03:00
|
|
|
if (exit_on_error)
|
2013-07-07 02:52:51 +03:00
|
|
|
exit_program(1);
|
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;
|
|
|
|
}
|
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];
|
|
|
|
close_output_stream(ost);
|
|
|
|
of_output_packet(of, ost->pkt, ost, 1);
|
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
|
|
|
|
2021-01-30 00:09:18 +02:00
|
|
|
ist->data_size += pkt->size;
|
2014-03-24 16:44:22 +03:00
|
|
|
ist->nb_packets++;
|
2014-03-17 12:58:05 +03:00
|
|
|
|
2012-08-09 19:04:17 +03:00
|
|
|
if (ist->discard)
|
|
|
|
goto discard_packet;
|
|
|
|
|
2014-03-24 16:44:22 +03:00
|
|
|
/* add the stream-global side data to the first packet */
|
2014-08-29 00:34:20 +03:00
|
|
|
if (ist->nb_packets == 1) {
|
2014-03-24 16:44:22 +03:00
|
|
|
for (i = 0; i < ist->st->nb_side_data; i++) {
|
|
|
|
AVPacketSideData *src_sd = &ist->st->side_data[i];
|
|
|
|
uint8_t *dst_data;
|
|
|
|
|
avformat, ffmpeg: deprecate old rotation API
The old "API" that signaled rotation as a metadata value has been
replaced by DISPLAYMATRIX side data quite a while ago.
There is no reason to make muxers/demuxers/API users support both. In
addition, the metadata API is dangerous, as user tags could "leak" into
it, creating unintended features or bugs.
ffmpeg CLI has to be updated to use the new API. In particular, we must
not allow to leak the "rotate" tag into the muxer. Some muxers will
catch this properly (like mov), but others (like mkv) can add it as
generic tag. Note applications, which use libavformat and assume the
old rotate API, will interpret such "rotate" user tags as rotate
metadata (which it is not), and incorrectly rotate the video.
The ffmpeg/ffplay tools drop the use of the old API for muxing and
demuxing, as all muxers/demuxers support the new API. This will mean
that the tools will not mistakenly interpret per-track "rotate" user
tags as rotate metadata. It will _not_ be treated as regression.
Unfortunately, hacks have been added, that allow the user to override
rotation by setting metadata explicitly, e.g. via
-metadata:s:v:0 rotate=0
See references to trac #4560. fate-filter-meta-4560-rotate0 tests this.
It's easier to adjust the hack for supporting it than arguing for its
removal, so ffmpeg CLI now explicitly catches this case, and essentially
replaces the "rotate" value with a display matrix side data. (It would
be easier for both user and implementation to create an explicit option
for rotation.)
When the code under FF_API_OLD_ROTATE_API is disabled, one FATE
reference file has to be updated (because "rotate" is not exported
anymore).
Tested-by: Michael Niedermayer <michael@niedermayer.cc>
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
2017-03-21 09:02:58 +02:00
|
|
|
if (src_sd->type == AV_PKT_DATA_DISPLAYMATRIX)
|
2014-03-24 16:44:22 +03:00
|
|
|
continue;
|
avformat, ffmpeg: deprecate old rotation API
The old "API" that signaled rotation as a metadata value has been
replaced by DISPLAYMATRIX side data quite a while ago.
There is no reason to make muxers/demuxers/API users support both. In
addition, the metadata API is dangerous, as user tags could "leak" into
it, creating unintended features or bugs.
ffmpeg CLI has to be updated to use the new API. In particular, we must
not allow to leak the "rotate" tag into the muxer. Some muxers will
catch this properly (like mov), but others (like mkv) can add it as
generic tag. Note applications, which use libavformat and assume the
old rotate API, will interpret such "rotate" user tags as rotate
metadata (which it is not), and incorrectly rotate the video.
The ffmpeg/ffplay tools drop the use of the old API for muxing and
demuxing, as all muxers/demuxers support the new API. This will mean
that the tools will not mistakenly interpret per-track "rotate" user
tags as rotate metadata. It will _not_ be treated as regression.
Unfortunately, hacks have been added, that allow the user to override
rotation by setting metadata explicitly, e.g. via
-metadata:s:v:0 rotate=0
See references to trac #4560. fate-filter-meta-4560-rotate0 tests this.
It's easier to adjust the hack for supporting it than arguing for its
removal, so ffmpeg CLI now explicitly catches this case, and essentially
replaces the "rotate" value with a display matrix side data. (It would
be easier for both user and implementation to create an explicit option
for rotation.)
When the code under FF_API_OLD_ROTATE_API is disabled, one FATE
reference file has to be updated (because "rotate" is not exported
anymore).
Tested-by: Michael Niedermayer <michael@niedermayer.cc>
Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
2017-03-21 09:02:58 +02:00
|
|
|
|
2021-01-30 00:09:18 +02:00
|
|
|
if (av_packet_get_side_data(pkt, src_sd->type, NULL))
|
2015-05-02 22:39:00 +02:00
|
|
|
continue;
|
2014-03-24 16:44:22 +03:00
|
|
|
|
2021-01-30 00:09:18 +02:00
|
|
|
dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
|
2014-03-24 16:44:22 +03:00
|
|
|
if (!dst_data)
|
2022-08-27 15:41:16 +02:00
|
|
|
report_and_exit(AVERROR(ENOMEM));
|
2014-03-24 16:44:22 +03:00
|
|
|
|
|
|
|
memcpy(dst_data, src_sd->data, src_sd->size);
|
|
|
|
}
|
2014-08-29 00:34:20 +03:00
|
|
|
}
|
2014-03-24 16:44:22 +03:00
|
|
|
|
2022-08-10 17:37:12 +02:00
|
|
|
// detect and try to correct for timestamp discontinuities
|
|
|
|
ts_discontinuity_process(ifile, ist, pkt);
|
2012-08-09 19:04:17 +03:00
|
|
|
|
2012-11-17 13:12:04 +03:00
|
|
|
if (debug_ts) {
|
2022-11-17 10:49:27 +02:00
|
|
|
av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
|
|
|
|
ifile->index, pkt->stream_index,
|
2022-08-13 17:03:39 +02:00
|
|
|
av_get_media_type_string(ist->par->codec_type),
|
2023-04-17 13:41:37 +02:00
|
|
|
av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &pkt->time_base),
|
|
|
|
av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &pkt->time_base),
|
|
|
|
av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &pkt->time_base),
|
2012-11-22 02:12:18 +03:00
|
|
|
av_ts2str(input_files[ist->file_index]->ts_offset),
|
|
|
|
av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
|
2012-11-17 13:12:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:09:18 +02:00
|
|
|
sub2video_heartbeat(ist, pkt->pts);
|
2012-08-09 19:04:17 +03:00
|
|
|
|
2021-01-30 00:09:18 +02:00
|
|
|
process_input_packet(ist, pkt, 0);
|
2012-08-09 19:04:17 +03:00
|
|
|
|
|
|
|
discard_packet:
|
2022-03-21 16:53:38 +02:00
|
|
|
av_packet_free(&pkt);
|
2012-08-09 19:04:17 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-17 14:50:16 +03:00
|
|
|
/**
|
|
|
|
* Run a single step of transcoding.
|
|
|
|
*
|
|
|
|
* @return 0 for success, <0 for error
|
|
|
|
*/
|
|
|
|
static int transcode_step(void)
|
|
|
|
{
|
|
|
|
OutputStream *ost;
|
2016-05-27 12:14:33 +02:00
|
|
|
InputStream *ist = NULL;
|
2012-08-17 14:50:16 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ost = choose_output();
|
|
|
|
if (!ost) {
|
|
|
|
if (got_eagain()) {
|
|
|
|
reset_eagain();
|
|
|
|
av_usleep(10000);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from, finishing.\n");
|
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
|
|
|
|
2016-05-27 12:14:33 +02:00
|
|
|
if (ost->filter && !ost->filter->graph->graph) {
|
|
|
|
if (ifilter_has_all_input_formats(ost->filter->graph)) {
|
|
|
|
ret = configure_filtergraph(ost->filter->graph);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ost->filter && ost->filter->graph->graph) {
|
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;
|
2016-05-27 12:14:33 +02:00
|
|
|
} else if (ost->filter) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < ost->filter->graph->nb_inputs; i++) {
|
|
|
|
InputFilter *ifilter = ost->filter->graph->inputs[i];
|
|
|
|
if (!ifilter->ist->got_output && !input_files[ifilter->ist->file_index]->eof_reached) {
|
|
|
|
ist = ifilter->ist;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ist) {
|
|
|
|
ost->inputs_done = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-17 14:50:16 +03:00
|
|
|
} 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;
|
|
|
|
|
2014-11-19 13:41:16 +02:00
|
|
|
return reap_filters(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
|
|
|
|
*/
|
2012-04-15 16:03:07 +03:00
|
|
|
static int transcode(void)
|
2005-06-03 17:31:45 +03:00
|
|
|
{
|
2011-08-20 00:45:02 +03:00
|
|
|
int ret, i;
|
|
|
|
InputStream *ist;
|
|
|
|
int64_t timer_start;
|
2001-07-22 17:37:44 +03:00
|
|
|
|
2012-04-15 16:03:07 +03:00
|
|
|
ret = transcode_init();
|
2011-08-20 00:45:02 +03:00
|
|
|
if (ret < 0)
|
2023-03-28 09:31:56 +02:00
|
|
|
return ret;
|
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) {
|
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
|
|
|
|
2012-04-01 16:08:33 +03:00
|
|
|
/* check if there's any stream where output is still needed */
|
2012-06-01 11:44:11 +03:00
|
|
|
if (!need_output()) {
|
|
|
|
av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
|
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
|
|
|
|
2012-08-17 14:50:16 +03:00
|
|
|
ret = transcode_step();
|
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)) {
|
2017-10-31 04:07:15 +02:00
|
|
|
if (!input_files[ist->file_index]->eof_reached) {
|
2015-10-14 14:28:20 +02:00
|
|
|
process_input_packet(ist, NULL, 0);
|
2002-05-20 19:32:49 +03:00
|
|
|
}
|
2001-09-17 00:47:13 +03:00
|
|
|
}
|
2023-03-25 12:49:54 +02:00
|
|
|
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)
|
|
|
|
{
|
2021-12-11 15:25:14 +02:00
|
|
|
int ret;
|
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();
|
|
|
|
|
2013-07-08 05:34:11 +03:00
|
|
|
register_exit(ffmpeg_cleanup);
|
2012-09-05 10:03:56 +03:00
|
|
|
|
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)
|
2013-07-07 02:52:51 +03:00
|
|
|
exit_program(1);
|
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);
|
2013-07-07 02:52:51 +03:00
|
|
|
exit_program(1);
|
2009-12-21 04:15:46 +02:00
|
|
|
}
|
2009-12-13 15:49:14 +02:00
|
|
|
|
2007-09-16 21:08:51 +03:00
|
|
|
/* file converter / grab */
|
|
|
|
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");
|
2013-07-07 02:52:51 +03:00
|
|
|
exit_program(1);
|
2007-09-16 21:08:51 +03:00
|
|
|
}
|
|
|
|
|
2018-04-27 19:31:59 +02:00
|
|
|
current_time = ti = get_benchmark_time_stamps();
|
2023-03-28 09:59:24 +02:00
|
|
|
ret = transcode();
|
|
|
|
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
|
|
|
}
|
2013-04-10 02:47:03 +03:00
|
|
|
av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" frames successfully decoded, %"PRIu64" decoding errors\n",
|
|
|
|
decode_error_stat[0], decode_error_stat[1]);
|
2013-10-01 19:17:16 +03:00
|
|
|
if ((decode_error_stat[0] + decode_error_stat[1]) * max_error_rate < decode_error_stat[1])
|
2013-10-01 19:28:11 +03:00
|
|
|
exit_program(69);
|
2007-09-16 21:08:51 +03:00
|
|
|
|
2023-03-28 09:59:24 +02:00
|
|
|
ret = received_nb_signals ? 255 : ret;
|
|
|
|
exit_program(ret);
|
|
|
|
return ret;
|
2007-09-16 21:08:51 +03:00
|
|
|
}
|