/* * Copyright (c) 2000-2003 Fabrice Bellard * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** * @file * multimedia converter based on the FFmpeg libraries */ #include "config.h"#include <ctype.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <errno.h>#include <limits.h>#include <stdatomic.h>#include <stdint.h> #if HAVE_IO_H#include <io.h>#endif#if HAVE_UNISTD_H#include <unistd.h>#endif #include "libavformat/avformat.h"#include "libavdevice/avdevice.h"#include "libswresample/swresample.h"#include "libavutil/opt.h"#include "libavutil/channel_layout.h"#include "libavutil/parseutils.h"#include "libavutil/samplefmt.h"#include "libavutil/fifo.h"#include "libavutil/hwcontext.h"#include "libavutil/internal.h"#include "libavutil/intreadwrite.h"#include "libavutil/dict.h"#include "libavutil/display.h"#include "libavutil/mathematics.h"#include "libavutil/pixdesc.h"#include "libavutil/avstring.h"#include "libavutil/libm.h"#include "libavutil/imgutils.h"#include "libavutil/timestamp.h"#include "libavutil/bprint.h"#include "libavutil/time.h"#include "libavutil/threadmessage.h"#include "libavcodec/mathops.h"#include "libavformat/os_support.h" # include "libavfilter/avfilter.h"# include "libavfilter/buffersrc.h"# include "libavfilter/buffersink.h" #if HAVE_SYS_RESOURCE_H#include <sys/time.h>#include <sys/types.h>#include <sys/resource.h>#elif HAVE_GETPROCESSTIMES#include <windows.h>#endif#if HAVE_GETPROCESSMEMORYINFO#include <windows.h>#include <psapi.h>#endif#if HAVE_SETCONSOLECTRLHANDLER#include <windows.h>#endif #if HAVE_SYS_SELECT_H#include <sys/select.h>#endif #if HAVE_TERMIOS_H#include <fcntl.h>#include <sys/ioctl.h>#include <sys/time.h>#include <termios.h>#elif HAVE_KBHIT#include <conio.h>#endif #if HAVE_PTHREADS#include <pthread.h>#endif #include <time.h> #include "ffmpeg.h"#include "cmdutils.h" #include "libavutil/avassert.h" const char program_name[] = "ffmpeg";const int program_birth_year = 2000; static FILE *vstats_file; const char *const forced_keyframes_const_names[] = { "n", "n_forced", "prev_forced_n", "prev_forced_t", "t", NULL}; static void do_video_stats(OutputStream *ost, int frame_size);static int64_t getutime(void);static int64_t getmaxrss(void);static int ifilter_has_all_input_formats(FilterGraph *fg); static int run_as_daemon = 0;static int nb_frames_dup = 0;static unsigned dup_warning = 1000;static int nb_frames_drop = 0;static int64_t decode_error_stat[2]; static int want_sdp = 1; static int current_time;AVIOContext *progress_avio = NULL; static uint8_t *subtitle_out; InputStream **input_streams = NULL;int nb_input_streams = 0;InputFile **input_files = NULL;int nb_input_files = 0; OutputStream **output_streams = NULL;int nb_output_streams = 0;OutputFile **output_files = NULL;int nb_output_files = 0; FilterGraph **filtergraphs;int nb_filtergraphs; #if HAVE_TERMIOS_H /* init terminal so that we can grab keys */static struct termios oldtty;static int restore_tty;#endif #if HAVE_PTHREADSstatic void free_input_threads(void);#endif /* 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. */ static int sub2video_get_blank_frame(InputStream *ist){ int ret; AVFrame *frame = ist->sub2video.frame; av_frame_unref(frame); 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; ist->sub2video.frame->format = AV_PIX_FMT_RGB32; if ((ret = av_frame_get_buffer(frame, 32)) < 0) return ret; memset(frame->data[0], 0, frame->height * frame->linesize[0]); return 0;} 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) { 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 ); return; } dst += r->y * dst_linesize + r->x * 4; src = r->data[0]; pal = (uint32_t *)r->data[1]; 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; src += r->linesize[0]; }} static void sub2video_push_ref(InputStream *ist, int64_t pts){ AVFrame *frame = ist->sub2video.frame; int i; av_assert1(frame->data[0]); ist->sub2video.last_pts = frame->pts = pts; for (i = 0; i < ist->nb_filters; i++) av_buffersrc_add_frame_flags(ist->filters[i]->filter, frame, AV_BUFFERSRC_FLAG_KEEP_REF | AV_BUFFERSRC_FLAG_PUSH);} void sub2video_update(InputStream *ist, AVSubtitle *sub){ AVFrame *frame = ist->sub2video.frame; int8_t *dst; int dst_linesize; int num_rects, i; int64_t pts, end_pts; if (!frame) return; if (sub) { pts = av_rescale_q(sub->pts + sub->start_display_time * 1000LL, AV_TIME_BASE_Q, ist->st->time_base); end_pts = av_rescale_q(sub->pts + sub->end_display_time * 1000LL, AV_TIME_BASE_Q, ist->st->time_base); num_rects = sub->num_rects; } else { pts = ist->sub2video.end_pts; end_pts = INT64_MAX; num_rects = 0; } if (sub2video_get_blank_frame(ist) < 0) { av_log(ist->dec_ctx, AV_LOG_ERROR, "Impossible to get a blank canvas.\n"); return; } dst = frame->data [0]; dst_linesize = frame->linesize[0]; for (i = 0; i < num_rects; i++) sub2video_copy_rect(dst, dst_linesize, frame->width, frame->height, sub->rects[i]); sub2video_push_ref(ist, pts); ist->sub2video.end_pts = end_pts;} 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++) { InputStream *ist2 = input_streams[infile->ist_index + i]; if (!ist2->sub2video.frame) continue; /* subtitles seem to be usually muxed ahead of other streams; if not, subtracting a larger time here is necessary */ 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; if (pts2 >= ist2->sub2video.end_pts || !ist2->sub2video.frame->data[0]) sub2video_update(ist2, NULL); 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; if (ist->sub2video.end_pts < INT64_MAX) sub2video_update(ist, NULL); for (i = 0; i < ist->nb_filters; i++) av_buffersrc_add_frame(ist->filters[i]->filter, NULL);} /* end of sub2video hack */ static void term_exit_sigsafe(void){#if HAVE_TERMIOS_H if(restore_tty) tcsetattr (0, TCSANOW, &oldtty);#endif} void term_exit(void){ av_log(NULL, AV_LOG_QUIET, "%s", ""); term_exit_sigsafe();} static volatile int received_sigterm = 0;static volatile int received_nb_signals = 0;static atomic_int transcode_init_done = ATOMIC_VAR_INIT(0);static volatile int ffmpeg_exited = 0;static int main_return_code = 0; static voidsigterm_handler(int sig){ received_sigterm = sig; received_nb_signals++; term_exit_sigsafe(); if(received_nb_signals > 3) { write(2/*STDERR_FILENO*/, "Received > 3 system signals, hard exiting\n", strlen("Received > 3 system signals, hard exiting\n")); exit(123); }} #if HAVE_SETCONSOLECTRLHANDLERstatic 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 void term_init(void){#if HAVE_TERMIOS_H if (!run_as_daemon && stdin_interaction) { struct termios tty; if (tcgetattr (0, &tty) == 0) { 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); } signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */ }#endif signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */ signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */#ifdef SIGXCPU signal(SIGXCPU, sigterm_handler);#endif#if HAVE_SETCONSOLECTRLHANDLER SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE);#endif} /* read a key without blocking */static int read_key(void){ unsigned char ch;#if HAVE_TERMIOS_H int n = 1; struct timeval tv; fd_set rfds; 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); } if (is_pipe) { /* When running under a GUI, you will end here. */ if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL)) { // input pipe may have been closed by the program that ran ffmpeg return -1; } //Read it if(nchars != 0) { read(0, &ch, 1); return ch; }else{ return -1; } }# endif if(kbhit()) return(getch());#endif return -1;} static int decode_interrupt_cb(void *ctx){ return received_nb_signals > atomic_load(&transcode_init_done);} const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL }; static void ffmpeg_cleanup(int ret){ int i, j; if (do_benchmark) { int maxrss = getmaxrss() / 1024; av_log(NULL, AV_LOG_INFO, "bench: maxrss=%ikB\n", maxrss); } for (i = 0; i < nb_filtergraphs; i++) { FilterGraph *fg = filtergraphs[i]; avfilter_graph_free(&fg->graph); for (j = 0; j < fg->nb_inputs; j++) { while (av_fifo_size(fg->inputs[j]->frame_queue)) { AVFrame *frame; av_fifo_generic_read(fg->inputs[j]->frame_queue, &frame, sizeof(frame), NULL); av_frame_free(&frame); } av_fifo_freep(&fg->inputs[j]->frame_queue); if (fg->inputs[j]->ist->sub2video.sub_queue) { while (av_fifo_size(fg->inputs[j]->ist->sub2video.sub_queue)) { AVSubtitle sub; av_fifo_generic_read(fg->inputs[j]->ist->sub2video.sub_queue, &sub, sizeof(sub), NULL); avsubtitle_free(&sub); } av_fifo_freep(&fg->inputs[j]->ist->sub2video.sub_queue); } av_buffer_unref(&fg->inputs[j]->hw_frames_ctx); av_freep(&fg->inputs[j]->name); av_freep(&fg->inputs[j]); } av_freep(&fg->inputs); for (j = 0; j < fg->nb_outputs; j++) {Lines 1–500 of 4820. Load more
/* * Copyright (c) 2000-2003 Fabrice Bellard * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** * @file * multimedia converter based on the FFmpeg libraries */ #include "config.h" #include <ctype.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <errno.h> #include <limits.h> #include <stdatomic.h> #include <stdint.h> #if HAVE_IO_H #include <io.h> #endif #if HAVE_UNISTD_H #include <unistd.h> #endif #include "libavformat/avformat.h" #include "libavdevice/avdevice.h" #include "libswresample/swresample.h" #include "libavutil/opt.h" #include "libavutil/channel_layout.h" #include "libavutil/parseutils.h" #include "libavutil/samplefmt.h" #include "libavutil/fifo.h" #include "libavutil/hwcontext.h" #include "libavutil/internal.h" #include "libavutil/intreadwrite.h" #include "libavutil/dict.h" #include "libavutil/display.h" #include "libavutil/mathematics.h" #include "libavutil/pixdesc.h" #include "libavutil/avstring.h" #include "libavutil/libm.h" #include "libavutil/imgutils.h" #include "libavutil/timestamp.h" #include "libavutil/bprint.h" #include "libavutil/time.h" #include "libavutil/threadmessage.h" #include "libavcodec/mathops.h" #include "libavformat/os_support.h" # include "libavfilter/avfilter.h" # include "libavfilter/buffersrc.h" # include "libavfilter/buffersink.h" #if HAVE_SYS_RESOURCE_H #include <sys/time.h> #include <sys/types.h> #include <sys/resource.h> #elif HAVE_GETPROCESSTIMES #include <windows.h> #endif #if HAVE_GETPROCESSMEMORYINFO #include <windows.h> #include <psapi.h> #endif #if HAVE_SETCONSOLECTRLHANDLER #include <windows.h> #endif #if HAVE_SYS_SELECT_H #include <sys/select.h> #endif #if HAVE_TERMIOS_H #include <fcntl.h> #include <sys/ioctl.h> #include <sys/time.h> #include <termios.h> #elif HAVE_KBHIT #include <conio.h> #endif #if HAVE_PTHREADS #include <pthread.h> #endif #include <time.h> #include "ffmpeg.h" #include "cmdutils.h" #include "libavutil/avassert.h" const char program_name[] = "ffmpeg"; const int program_birth_year = 2000; static FILE *vstats_file; const char *const forced_keyframes_const_names[] = { "n", "n_forced", "prev_forced_n", "prev_forced_t", "t", NULL }; static void do_video_stats(OutputStream *ost, int frame_size); static int64_t getutime(void); static int64_t getmaxrss(void); static int ifilter_has_all_input_formats(FilterGraph *fg); static int run_as_daemon = 0; static int nb_frames_dup = 0; static unsigned dup_warning = 1000; static int nb_frames_drop = 0; static int64_t decode_error_stat[2]; static int want_sdp = 1; static int current_time; AVIOContext *progress_avio = NULL; static uint8_t *subtitle_out; InputStream **input_streams = NULL; int nb_input_streams = 0; InputFile **input_files = NULL; int nb_input_files = 0; OutputStream **output_streams = NULL; int nb_output_streams = 0; OutputFile **output_files = NULL; int nb_output_files = 0; FilterGraph **filtergraphs; int nb_filtergraphs; #if HAVE_TERMIOS_H /* init terminal so that we can grab keys */ static struct termios oldtty; static int restore_tty; #endif #if HAVE_PTHREADS static void free_input_threads(void); #endif /* 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. */ static int sub2video_get_blank_frame(InputStream *ist) { int ret; AVFrame *frame = ist->sub2video.frame; av_frame_unref(frame); 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; ist->sub2video.frame->format = AV_PIX_FMT_RGB32; if ((ret = av_frame_get_buffer(frame, 32)) < 0) return ret; memset(frame->data[0], 0, frame->height * frame->linesize[0]); return 0; } 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) { 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 ); return; } dst += r->y * dst_linesize + r->x * 4; src = r->data[0]; pal = (uint32_t *)r->data[1]; 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; src += r->linesize[0]; } } static void sub2video_push_ref(InputStream *ist, int64_t pts) { AVFrame *frame = ist->sub2video.frame; int i; av_assert1(frame->data[0]); ist->sub2video.last_pts = frame->pts = pts; for (i = 0; i < ist->nb_filters; i++) av_buffersrc_add_frame_flags(ist->filters[i]->filter, frame, AV_BUFFERSRC_FLAG_KEEP_REF | AV_BUFFERSRC_FLAG_PUSH); } void sub2video_update(InputStream *ist, AVSubtitle *sub) { AVFrame *frame = ist->sub2video.frame; int8_t *dst; int dst_linesize; int num_rects, i; int64_t pts, end_pts; if (!frame) return; if (sub) { pts = av_rescale_q(sub->pts + sub->start_display_time * 1000LL, AV_TIME_BASE_Q, ist->st->time_base); end_pts = av_rescale_q(sub->pts + sub->end_display_time * 1000LL, AV_TIME_BASE_Q, ist->st->time_base); num_rects = sub->num_rects; } else { pts = ist->sub2video.end_pts; end_pts = INT64_MAX; num_rects = 0; } if (sub2video_get_blank_frame(ist) < 0) { av_log(ist->dec_ctx, AV_LOG_ERROR, "Impossible to get a blank canvas.\n"); return; } dst = frame->data [0]; dst_linesize = frame->linesize[0]; for (i = 0; i < num_rects; i++) sub2video_copy_rect(dst, dst_linesize, frame->width, frame->height, sub->rects[i]); sub2video_push_ref(ist, pts); ist->sub2video.end_pts = end_pts; } 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++) { InputStream *ist2 = input_streams[infile->ist_index + i]; if (!ist2->sub2video.frame) continue; /* subtitles seem to be usually muxed ahead of other streams; if not, subtracting a larger time here is necessary */ 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; if (pts2 >= ist2->sub2video.end_pts || !ist2->sub2video.frame->data[0]) sub2video_update(ist2, NULL); 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; if (ist->sub2video.end_pts < INT64_MAX) sub2video_update(ist, NULL); for (i = 0; i < ist->nb_filters; i++) av_buffersrc_add_frame(ist->filters[i]->filter, NULL); } /* end of sub2video hack */ static void term_exit_sigsafe(void) { #if HAVE_TERMIOS_H if(restore_tty) tcsetattr (0, TCSANOW, &oldtty); #endif } void term_exit(void) { av_log(NULL, AV_LOG_QUIET, "%s", ""); term_exit_sigsafe(); } static volatile int received_sigterm = 0; static volatile int received_nb_signals = 0; static atomic_int transcode_init_done = ATOMIC_VAR_INIT(0); static volatile int ffmpeg_exited = 0; static int main_return_code = 0; static void sigterm_handler(int sig) { received_sigterm = sig; received_nb_signals++; term_exit_sigsafe(); if(received_nb_signals > 3) { write(2/*STDERR_FILENO*/, "Received > 3 system signals, hard exiting\n", strlen("Received > 3 system signals, hard exiting\n")); exit(123); } } #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 void term_init(void) { #if HAVE_TERMIOS_H if (!run_as_daemon && stdin_interaction) { struct termios tty; if (tcgetattr (0, &tty) == 0) { 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); } signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */ } #endif signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */ signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */ #ifdef SIGXCPU signal(SIGXCPU, sigterm_handler); #endif #if HAVE_SETCONSOLECTRLHANDLER SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE); #endif } /* read a key without blocking */ static int read_key(void) { unsigned char ch; #if HAVE_TERMIOS_H int n = 1; struct timeval tv; fd_set rfds; 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); } if (is_pipe) { /* When running under a GUI, you will end here. */ if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL)) { // input pipe may have been closed by the program that ran ffmpeg return -1; } //Read it if(nchars != 0) { read(0, &ch, 1); return ch; }else{ return -1; } } # endif if(kbhit()) return(getch()); #endif return -1; } static int decode_interrupt_cb(void *ctx) { return received_nb_signals > atomic_load(&transcode_init_done); } const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL }; static void ffmpeg_cleanup(int ret) { int i, j; if (do_benchmark) { int maxrss = getmaxrss() / 1024; av_log(NULL, AV_LOG_INFO, "bench: maxrss=%ikB\n", maxrss); } for (i = 0; i < nb_filtergraphs; i++) { FilterGraph *fg = filtergraphs[i]; avfilter_graph_free(&fg->graph); for (j = 0; j < fg->nb_inputs; j++) { while (av_fifo_size(fg->inputs[j]->frame_queue)) { AVFrame *frame; av_fifo_generic_read(fg->inputs[j]->frame_queue, &frame, sizeof(frame), NULL); av_frame_free(&frame); } av_fifo_freep(&fg->inputs[j]->frame_queue); if (fg->inputs[j]->ist->sub2video.sub_queue) { while (av_fifo_size(fg->inputs[j]->ist->sub2video.sub_queue)) { AVSubtitle sub; av_fifo_generic_read(fg->inputs[j]->ist->sub2video.sub_queue, &sub, sizeof(sub), NULL); avsubtitle_free(&sub); } av_fifo_freep(&fg->inputs[j]->ist->sub2video.sub_queue); } av_buffer_unref(&fg->inputs[j]->hw_frames_ctx); av_freep(&fg->inputs[j]->name); av_freep(&fg->inputs[j]); } av_freep(&fg->inputs); for (j = 0; j < fg->nb_outputs; j++) {