ffmpeg.c File Reference

multimedia converter based on the FFmpeg libraries More...

#include "config.h"
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavutil/opt.h"
#include "libavutil/channel_layout.h"
#include "libavutil/parseutils.h"
#include "libavutil/samplefmt.h"
#include "libavutil/colorspace.h"
#include "libavutil/fifo.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/dict.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 "libavformat/os_support.h"
#include "libavformat/ffm.h"
#include "libavfilter/avcodec.h"
#include "libavfilter/avfilter.h"
#include "libavfilter/buffersrc.h"
#include "libavfilter/buffersink.h"
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <pthread.h>
#include "ffmpeg.h"
#include "cmdutils.h"
#include "libavutil/avassert.h"
Include dependency graph for ffmpeg.c:

Go to the source code of this file.

Macros

#define DEFAULT_PASS_LOGFILENAME_PREFIX   "ffmpeg2pass"
 

Functions

static void do_video_stats (OutputStream *ost, int frame_size)
 
static int64_t getutime (void)
 
static int64_t getmaxrss (void)
 
static int sub2video_get_blank_frame (InputStream *ist)
 
static void sub2video_copy_rect (uint8_t *dst, int dst_linesize, int w, int h, AVSubtitleRect *r)
 
static void sub2video_push_ref (InputStream *ist, int64_t pts)
 
static void sub2video_update (InputStream *ist, AVSubtitle *sub)
 
static void sub2video_heartbeat (InputStream *ist, int64_t pts)
 
static void sub2video_flush (InputStream *ist)
 
void term_exit (void)
 
static void sigterm_handler (int sig)
 
void term_init (void)
 
static int read_key (void)
 
static int decode_interrupt_cb (void *ctx)
 
static void exit_program (void)
 
void assert_avoptions (AVDictionary *m)
 
static void abort_codec_experimental (AVCodec *c, int encoder)
 
static void update_benchmark (const char *fmt,...)
 
static void write_frame (AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
 
static void close_output_stream (OutputStream *ost)
 
static int check_recording_time (OutputStream *ost)
 
static void do_audio_out (AVFormatContext *s, OutputStream *ost, AVFrame *frame)
 
static void do_subtitle_out (AVFormatContext *s, OutputStream *ost, InputStream *ist, AVSubtitle *sub)
 
static void do_video_out (AVFormatContext *s, OutputStream *ost, AVFrame *in_picture)
 
static double psnr (double d)
 
static int reap_filters (void)
 Get and encode new output from any of the filtergraphs, without causing activity. More...
 
static void print_report (int is_last_report, int64_t timer_start, int64_t cur_time)
 
static void flush_encoders (void)
 
static int check_output_constraints (InputStream *ist, OutputStream *ost)
 
static void do_streamcopy (InputStream *ist, OutputStream *ost, const AVPacket *pkt)
 
static void rate_emu_sleep (InputStream *ist)
 
int guess_input_channel_layout (InputStream *ist)
 
static int decode_audio (InputStream *ist, AVPacket *pkt, int *got_output)
 
static int decode_video (InputStream *ist, AVPacket *pkt, int *got_output)
 
static int transcode_subtitles (InputStream *ist, AVPacket *pkt, int *got_output)
 
static int output_packet (InputStream *ist, const AVPacket *pkt)
 
static void print_sdp (void)
 
static int init_input_stream (int ist_index, char *error, int error_len)
 
static InputStreamget_input_stream (OutputStream *ost)
 
static int compare_int64 (const void *a, const void *b)
 
static void parse_forced_key_frames (char *kf, OutputStream *ost, AVCodecContext *avctx)
 
static void report_new_stream (int input_index, AVPacket *pkt)
 
static int transcode_init (void)
 
static int need_output (void)
 
static OutputStreamchoose_output (void)
 Select the output stream to process. More...
 
static int check_keyboard_interaction (int64_t cur_time)
 
static voidinput_thread (void *arg)
 
static void free_input_threads (void)
 
static int init_input_threads (void)
 
static int get_input_packet_mt (InputFile *f, AVPacket *pkt)
 
static int get_input_packet (InputFile *f, AVPacket *pkt)
 
static int got_eagain (void)
 
static void reset_eagain (void)
 
static int process_input (int file_index)
 
static int transcode_from_filter (FilterGraph *graph, InputStream **best_ist)
 Perform a step of transcoding for the specified filter graph. More...
 
static int transcode_step (void)
 Run a single step of transcoding. More...
 
static int transcode (void)
 
static void log_callback_null (void *ptr, int level, const char *fmt, va_list vl)
 
int main (int argc, char **argv)
 

Variables

const char program_name [] = "ffmpeg"
 program name, defined by the program for show_version(). More...
 
const int program_birth_year = 2000
 program birth year, defined by the program for show_banner() More...
 
static FILE * vstats_file
 
const char *const forced_keyframes_const_names []
 
static int run_as_daemon = 0
 
static int64_t video_size = 0
 
static int64_t audio_size = 0
 
static int64_t subtitle_size = 0
 
static int64_t extra_size = 0
 
static int nb_frames_dup = 0
 
static int nb_frames_drop = 0
 
static int64_t decode_error_stat [2]
 
static int current_time
 
AVIOContextprogress_avio = NULL
 
static uint8_tsubtitle_out
 
static int transcoding_finished
 
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
 
static struct termios oldtty
 
static int restore_tty
 
static volatile int received_sigterm = 0
 
static volatile int received_nb_signals = 0
 
const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL }
 

Detailed Description

multimedia converter based on the FFmpeg libraries

Definition in file ffmpeg.c.

Macro Definition Documentation

#define DEFAULT_PASS_LOGFILENAME_PREFIX   "ffmpeg2pass"

Definition at line 143 of file ffmpeg.c.

Referenced by transcode_init().

Function Documentation

static void abort_codec_experimental ( AVCodec c,
int  encoder 
)
static

Definition at line 519 of file ffmpeg.c.

Referenced by init_input_stream(), and transcode_init().

void assert_avoptions ( AVDictionary m)

Definition at line 510 of file ffmpeg.c.

Referenced by init_input_stream(), open_input_file(), and transcode_init().

static int check_keyboard_interaction ( int64_t  cur_time)
static

Definition at line 2616 of file ffmpeg.c.

Referenced by transcode().

static int check_output_constraints ( InputStream ist,
OutputStream ost 
)
static

Definition at line 1356 of file ffmpeg.c.

Referenced by output_packet(), and transcode_subtitles().

static int check_recording_time ( OutputStream ost)
static

Definition at line 643 of file ffmpeg.c.

Referenced by do_audio_out(), do_subtitle_out(), and do_video_out().

static OutputStream* choose_output ( void  )
static

Select the output stream to process.

Returns
selected output stream, or NULL if none available

Definition at line 2598 of file ffmpeg.c.

Referenced by transcode_step().

static void close_output_stream ( OutputStream ost)
static
static int compare_int64 ( const void a,
const void b 
)
static

Definition at line 1982 of file ffmpeg.c.

Referenced by parse_forced_key_frames().

static int decode_audio ( InputStream ist,
AVPacket pkt,
int *  got_output 
)
static

Definition at line 1484 of file ffmpeg.c.

Referenced by output_packet().

static int decode_interrupt_cb ( void ctx)
static

Definition at line 417 of file ffmpeg.c.

static int decode_video ( InputStream ist,
AVPacket pkt,
int *  got_output 
)
static

Definition at line 1624 of file ffmpeg.c.

Referenced by output_packet().

static void do_audio_out ( AVFormatContext s,
OutputStream ost,
AVFrame frame 
)
static

Definition at line 656 of file ffmpeg.c.

Referenced by reap_filters().

static void do_streamcopy ( InputStream ist,
OutputStream ost,
const AVPacket pkt 
)
static

Definition at line 1370 of file ffmpeg.c.

Referenced by output_packet().

static void do_subtitle_out ( AVFormatContext s,
OutputStream ost,
InputStream ist,
AVSubtitle sub 
)
static

Definition at line 704 of file ffmpeg.c.

Referenced by transcode_subtitles().

static void do_video_out ( AVFormatContext s,
OutputStream ost,
AVFrame in_picture 
)
static

Definition at line 775 of file ffmpeg.c.

Referenced by reap_filters().

static void do_video_stats ( OutputStream ost,
int  frame_size 
)
static

Definition at line 977 of file ffmpeg.c.

Referenced by do_video_out(), and flush_encoders().

static void exit_program ( void  )
static

Definition at line 424 of file ffmpeg.c.

Referenced by main().

static void flush_encoders ( void  )
static

Definition at line 1275 of file ffmpeg.c.

Referenced by transcode().

static void free_input_threads ( void  )
static

Definition at line 2740 of file ffmpeg.c.

Referenced by transcode().

static int get_input_packet ( InputFile f,
AVPacket pkt 
)
static

Definition at line 2819 of file ffmpeg.c.

Referenced by process_input().

static int get_input_packet_mt ( InputFile f,
AVPacket pkt 
)
static

Definition at line 2797 of file ffmpeg.c.

Referenced by get_input_packet().

static InputStream* get_input_stream ( OutputStream ost)
static

Definition at line 1975 of file ffmpeg.c.

Referenced by transcode_init().

static int64_t getmaxrss ( void  )
static

Definition at line 3285 of file ffmpeg.c.

Referenced by exit_program().

static int64_t getutime ( void  )
static

Definition at line 3267 of file ffmpeg.c.

Referenced by main(), and update_benchmark().

static int got_eagain ( void  )
static

Definition at line 2828 of file ffmpeg.c.

Referenced by transcode_step().

int guess_input_channel_layout ( InputStream ist)

Definition at line 1464 of file ffmpeg.c.

Referenced by add_input_streams(), and decode_audio().

static int init_input_stream ( int  ist_index,
char *  error,
int  error_len 
)
static

Definition at line 1941 of file ffmpeg.c.

Referenced by transcode_init().

static int init_input_threads ( void  )
static

Definition at line 2775 of file ffmpeg.c.

Referenced by transcode().

static void* input_thread ( void arg)
static

Definition at line 2710 of file ffmpeg.c.

Referenced by init_input_threads().

static void log_callback_null ( void ptr,
int  level,
const char *  fmt,
va_list  vl 
)
static

Definition at line 3303 of file ffmpeg.c.

Referenced by main().

int main ( int  argc,
char **  argv 
)

Definition at line 3307 of file ffmpeg.c.

static int need_output ( void  )
static

Definition at line 2568 of file ffmpeg.c.

Referenced by transcode().

static int output_packet ( InputStream ist,
const AVPacket pkt 
)
static

Definition at line 1784 of file ffmpeg.c.

Referenced by process_input(), and transcode().

static void parse_forced_key_frames ( char *  kf,
OutputStream ost,
AVCodecContext avctx 
)
static

Definition at line 1988 of file ffmpeg.c.

Referenced by transcode_init().

static void print_report ( int  is_last_report,
int64_t  timer_start,
int64_t  cur_time 
)
static

Definition at line 1098 of file ffmpeg.c.

Referenced by transcode().

static void print_sdp ( void  )
static

Definition at line 1924 of file ffmpeg.c.

Referenced by transcode_init().

static int process_input ( int  file_index)
static

Definition at line 2853 of file ffmpeg.c.

Referenced by transcode_step().

static double psnr ( double  d)
static

Definition at line 972 of file ffmpeg.c.

Referenced by do_video_stats(), main(), print_report(), and run_psnr().

static void rate_emu_sleep ( InputStream ist)
static

Definition at line 1454 of file ffmpeg.c.

Referenced by decode_audio(), decode_video(), output_packet(), and transcode_subtitles().

static int read_key ( void  )
static

Definition at line 362 of file ffmpeg.c.

Referenced by check_keyboard_interaction().

static int reap_filters ( void  )
static

Get and encode new output from any of the filtergraphs, without causing activity.

Returns
0 for success, <0 for severe errors

Definition at line 1019 of file ffmpeg.c.

Referenced by transcode_from_filter(), and transcode_step().

static void report_new_stream ( int  input_index,
AVPacket pkt 
)
static

Definition at line 2051 of file ffmpeg.c.

Referenced by process_input().

static void reset_eagain ( void  )
static

Definition at line 2837 of file ffmpeg.c.

Referenced by process_input(), and transcode_step().

static void sigterm_handler ( int  sig)
static

Definition at line 315 of file ffmpeg.c.

Referenced by term_init().

static void sub2video_copy_rect ( uint8_t dst,
int  dst_linesize,
int  w,
int  h,
AVSubtitleRect r 
)
static

Definition at line 186 of file ffmpeg.c.

Referenced by sub2video_update().

static void sub2video_flush ( InputStream ist)
static

Definition at line 292 of file ffmpeg.c.

Referenced by transcode_subtitles().

static int sub2video_get_blank_frame ( InputStream ist)
static

Definition at line 171 of file ffmpeg.c.

Referenced by sub2video_update().

static void sub2video_heartbeat ( InputStream ist,
int64_t  pts 
)
static

Definition at line 263 of file ffmpeg.c.

Referenced by process_input().

static void sub2video_push_ref ( InputStream ist,
int64_t  pts 
)
static

Definition at line 215 of file ffmpeg.c.

Referenced by sub2video_heartbeat(), and sub2video_update().

static void sub2video_update ( InputStream ist,
AVSubtitle sub 
)
static

Definition at line 228 of file ffmpeg.c.

Referenced by sub2video_heartbeat(), and transcode_subtitles().

void term_exit ( void  )

Definition at line 302 of file ffmpeg.c.

Referenced by assert_file_overwrite(), sigterm_handler(), term_init(), and transcode().

void term_init ( void  )

Definition at line 324 of file ffmpeg.c.

Referenced by assert_file_overwrite(), and main().

static int transcode ( void  )
static

Definition at line 3147 of file ffmpeg.c.

Referenced by main().

static int transcode_from_filter ( FilterGraph graph,
InputStream **  best_ist 
)
static

Perform a step of transcoding for the specified filter graph.

Parameters
[in]graphfilter graph to consider
[out]best_istinput stream where a frame would allow to continue
Returns
0 for success, <0 for error

Definition at line 3059 of file ffmpeg.c.

Referenced by transcode_step().

static int transcode_init ( void  )
static

Definition at line 2066 of file ffmpeg.c.

Referenced by transcode().

static int transcode_step ( void  )
static

Run a single step of transcoding.

Returns
0 for success, <0 for error

Definition at line 3105 of file ffmpeg.c.

Referenced by transcode().

static int transcode_subtitles ( InputStream ist,
AVPacket pkt,
int *  got_output 
)
static

Definition at line 1732 of file ffmpeg.c.

Referenced by output_packet().

static void update_benchmark ( const char *  fmt,
  ... 
)
static

Definition at line 524 of file ffmpeg.c.

Referenced by decode_audio(), decode_video(), do_audio_out(), do_video_out(), and flush_encoders().

static void write_frame ( AVFormatContext s,
AVPacket pkt,
OutputStream ost 
)
static

Definition at line 541 of file ffmpeg.c.

Referenced by do_audio_out(), do_streamcopy(), do_subtitle_out(), do_video_out(), and flush_encoders().

Variable Documentation

int current_time
static

Definition at line 133 of file ffmpeg.c.

Referenced by main(), and update_benchmark().

int64_t decode_error_stat[2]
static

Definition at line 131 of file ffmpeg.c.

Referenced by decode_audio(), decode_video(), main(), and transcode_subtitles().

int64_t extra_size = 0
static

Definition at line 128 of file ffmpeg.c.

Referenced by ape_read_packet(), asf_write_header1(), print_report(), and transcode_init().

const char* const forced_keyframes_const_names[]
Initial value:
= {
"n",
"n_forced",
"prev_forced_n",
"prev_forced_t",
"t",
}
NULL
Definition: eval.c:55

Definition at line 111 of file ffmpeg.c.

Referenced by transcode_init().

int nb_frames_drop = 0
static

Definition at line 130 of file ffmpeg.c.

Referenced by do_video_out(), and print_report().

int nb_frames_dup = 0
static

Definition at line 129 of file ffmpeg.c.

Referenced by do_video_out(), and print_report().

int nb_output_files = 0
struct termios oldtty
static

Definition at line 161 of file ffmpeg.c.

Referenced by term_exit(), and term_init().

OutputFile** output_files = NULL

Definition at line 152 of file ffmpeg.c.

Referenced by open_output_file().

OutputStream** output_streams = NULL

Definition at line 150 of file ffmpeg.c.

Referenced by new_output_stream(), and open_output_file().

const int program_birth_year = 2000

program birth year, defined by the program for show_banner()

Definition at line 107 of file ffmpeg.c.

const char program_name[] = "ffmpeg"

program name, defined by the program for show_version().

Definition at line 106 of file ffmpeg.c.

Referenced by main().

AVIOContext* progress_avio = NULL

Definition at line 134 of file ffmpeg.c.

Referenced by opt_progress().

volatile int received_nb_signals = 0
static

Definition at line 312 of file ffmpeg.c.

Referenced by check_keyboard_interaction(), decode_interrupt_cb(), main(), and sigterm_handler().

volatile int received_sigterm = 0
static

Definition at line 311 of file ffmpeg.c.

Referenced by exit_program(), sigterm_handler(), and transcode().

int restore_tty
static

Definition at line 162 of file ffmpeg.c.

Referenced by term_exit(), and term_init().

int run_as_daemon = 0
static

Definition at line 124 of file ffmpeg.c.

Referenced by check_keyboard_interaction(), main(), and term_init().

uint8_t* subtitle_out
static

Definition at line 136 of file ffmpeg.c.

Referenced by do_subtitle_out(), and exit_program().

int64_t subtitle_size = 0
static

Definition at line 127 of file ffmpeg.c.

Referenced by do_streamcopy(), do_subtitle_out(), and print_report().

int transcoding_finished
static

Definition at line 140 of file ffmpeg.c.

Referenced by free_input_threads(), and input_thread().

int64_t video_size = 0
static
FILE* vstats_file
static

Definition at line 109 of file ffmpeg.c.

Referenced by do_video_stats(), and exit_program().