| 1 | /* |
| 2 | * This file is part of FFmpeg. |
| 3 | * |
| 4 | * FFmpeg is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * FFmpeg is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with FFmpeg; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #ifndef FFMPEG_H |
| 20 | #define FFMPEG_H |
| 21 | |
| 22 | #include "config.h" |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | #include <stdio.h> |
| 26 | #include <signal.h> |
| 27 | |
| 28 | #if HAVE_PTHREADS |
| 29 | #include <pthread.h> |
| 30 | #endif |
| 31 | |
| 32 | #include "cmdutils.h" |
| 33 | |
| 34 | #include "libavformat/avformat.h" |
| 35 | #include "libavformat/avio.h" |
| 36 | |
| 37 | #include "libavcodec/avcodec.h" |
| 38 | |
| 39 | #include "libavfilter/avfilter.h" |
| 40 | |
| 41 | #include "libavutil/avutil.h" |
| 42 | #include "libavutil/dict.h" |
| 43 | #include "libavutil/eval.h" |
| 44 | #include "libavutil/fifo.h" |
| 45 | #include "libavutil/pixfmt.h" |
| 46 | #include "libavutil/rational.h" |
| 47 | #include "libavutil/threadmessage.h" |
| 48 | |
| 49 | #include "libswresample/swresample.h" |
| 50 | |
| 51 | #define VSYNC_AUTO -1 |
| 52 | #define VSYNC_PASSTHROUGH 0 |
| 53 | #define VSYNC_CFR 1 |
| 54 | #define VSYNC_VFR 2 |
| 55 | #define VSYNC_VSCFR 0xfe |
| 56 | #define VSYNC_DROP 0xff |
| 57 | |
| 58 | #define MAX_STREAMS 1024 /* arbitrary sanity check value */ |
| 59 | |
| 60 | enum HWAccelID { |
| 61 | HWACCEL_NONE = 0, |
| 62 | HWACCEL_AUTO, |
| 63 | HWACCEL_VDPAU, |
| 64 | HWACCEL_DXVA2, |
| 65 | HWACCEL_VDA, |
| 66 | }; |
| 67 | |
| 68 | typedef struct HWAccel { |
| 69 | const char *name; |
| 70 | int (*init)(AVCodecContext *s); |
| 71 | enum HWAccelID id; |
| 72 | enum AVPixelFormat pix_fmt; |
| 73 | } HWAccel; |
| 74 | |
| 75 | /* select an input stream for an output stream */ |
| 76 | typedef struct StreamMap { |
| 77 | int disabled; /* 1 is this mapping is disabled by a negative map */ |
| 78 | int file_index; |
| 79 | int stream_index; |
| 80 | int sync_file_index; |
| 81 | int sync_stream_index; |
| 82 | char *linklabel; /* name of an output link, for mapping lavfi outputs */ |
| 83 | } StreamMap; |
| 84 | |
| 85 | typedef struct { |
| 86 | int file_idx, stream_idx, channel_idx; // input |
| 87 | int ofile_idx, ostream_idx; // output |
| 88 | } AudioChannelMap; |
| 89 | |
| 90 | typedef struct OptionsContext { |
| 91 | OptionGroup *g; |
| 92 | |
| 93 | /* input/output options */ |
| 94 | int64_t start_time; |
| 95 | const char *format; |
| 96 | |
| 97 | SpecifierOpt *codec_names; |
| 98 | int nb_codec_names; |
| 99 | SpecifierOpt *audio_channels; |
| 100 | int nb_audio_channels; |
| 101 | SpecifierOpt *audio_sample_rate; |
| 102 | int nb_audio_sample_rate; |
| 103 | SpecifierOpt *frame_rates; |
| 104 | int nb_frame_rates; |
| 105 | SpecifierOpt *frame_sizes; |
| 106 | int nb_frame_sizes; |
| 107 | SpecifierOpt *frame_pix_fmts; |
| 108 | int nb_frame_pix_fmts; |
| 109 | |
| 110 | /* input options */ |
| 111 | int64_t input_ts_offset; |
| 112 | int rate_emu; |
| 113 | int accurate_seek; |
| 114 | |
| 115 | SpecifierOpt *ts_scale; |
| 116 | int nb_ts_scale; |
| 117 | SpecifierOpt *dump_attachment; |
| 118 | int nb_dump_attachment; |
| 119 | SpecifierOpt *hwaccels; |
| 120 | int nb_hwaccels; |
| 121 | SpecifierOpt *hwaccel_devices; |
| 122 | int nb_hwaccel_devices; |
| 123 | |
| 124 | /* output options */ |
| 125 | StreamMap *stream_maps; |
| 126 | int nb_stream_maps; |
| 127 | AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */ |
| 128 | int nb_audio_channel_maps; /* number of (valid) -map_channel settings */ |
| 129 | int metadata_global_manual; |
| 130 | int metadata_streams_manual; |
| 131 | int metadata_chapters_manual; |
| 132 | const char **attachments; |
| 133 | int nb_attachments; |
| 134 | |
| 135 | int chapters_input_file; |
| 136 | |
| 137 | int64_t recording_time; |
| 138 | int64_t stop_time; |
| 139 | uint64_t limit_filesize; |
| 140 | float mux_preload; |
| 141 | float mux_max_delay; |
| 142 | int shortest; |
| 143 | |
| 144 | int video_disable; |
| 145 | int audio_disable; |
| 146 | int subtitle_disable; |
| 147 | int data_disable; |
| 148 | |
| 149 | /* indexed by output file stream index */ |
| 150 | int *streamid_map; |
| 151 | int nb_streamid_map; |
| 152 | |
| 153 | SpecifierOpt *metadata; |
| 154 | int nb_metadata; |
| 155 | SpecifierOpt *max_frames; |
| 156 | int nb_max_frames; |
| 157 | SpecifierOpt *bitstream_filters; |
| 158 | int nb_bitstream_filters; |
| 159 | SpecifierOpt *codec_tags; |
| 160 | int nb_codec_tags; |
| 161 | SpecifierOpt *sample_fmts; |
| 162 | int nb_sample_fmts; |
| 163 | SpecifierOpt *qscale; |
| 164 | int nb_qscale; |
| 165 | SpecifierOpt *forced_key_frames; |
| 166 | int nb_forced_key_frames; |
| 167 | SpecifierOpt *force_fps; |
| 168 | int nb_force_fps; |
| 169 | SpecifierOpt *frame_aspect_ratios; |
| 170 | int nb_frame_aspect_ratios; |
| 171 | SpecifierOpt *rc_overrides; |
| 172 | int nb_rc_overrides; |
| 173 | SpecifierOpt *intra_matrices; |
| 174 | int nb_intra_matrices; |
| 175 | SpecifierOpt *inter_matrices; |
| 176 | int nb_inter_matrices; |
| 177 | SpecifierOpt *chroma_intra_matrices; |
| 178 | int nb_chroma_intra_matrices; |
| 179 | SpecifierOpt *top_field_first; |
| 180 | int nb_top_field_first; |
| 181 | SpecifierOpt *metadata_map; |
| 182 | int nb_metadata_map; |
| 183 | SpecifierOpt *presets; |
| 184 | int nb_presets; |
| 185 | SpecifierOpt *copy_initial_nonkeyframes; |
| 186 | int nb_copy_initial_nonkeyframes; |
| 187 | SpecifierOpt *copy_prior_start; |
| 188 | int nb_copy_prior_start; |
| 189 | SpecifierOpt *filters; |
| 190 | int nb_filters; |
| 191 | SpecifierOpt *filter_scripts; |
| 192 | int nb_filter_scripts; |
| 193 | SpecifierOpt *reinit_filters; |
| 194 | int nb_reinit_filters; |
| 195 | SpecifierOpt *fix_sub_duration; |
| 196 | int nb_fix_sub_duration; |
| 197 | SpecifierOpt *canvas_sizes; |
| 198 | int nb_canvas_sizes; |
| 199 | SpecifierOpt *pass; |
| 200 | int nb_pass; |
| 201 | SpecifierOpt *passlogfiles; |
| 202 | int nb_passlogfiles; |
| 203 | SpecifierOpt *guess_layout_max; |
| 204 | int nb_guess_layout_max; |
| 205 | SpecifierOpt *apad; |
| 206 | int nb_apad; |
| 207 | SpecifierOpt *discard; |
| 208 | int nb_discard; |
| 209 | } OptionsContext; |
| 210 | |
| 211 | typedef struct InputFilter { |
| 212 | AVFilterContext *filter; |
| 213 | struct InputStream *ist; |
| 214 | struct FilterGraph *graph; |
| 215 | uint8_t *name; |
| 216 | } InputFilter; |
| 217 | |
| 218 | typedef struct OutputFilter { |
| 219 | AVFilterContext *filter; |
| 220 | struct OutputStream *ost; |
| 221 | struct FilterGraph *graph; |
| 222 | uint8_t *name; |
| 223 | |
| 224 | /* temporary storage until stream maps are processed */ |
| 225 | AVFilterInOut *out_tmp; |
| 226 | } OutputFilter; |
| 227 | |
| 228 | typedef struct FilterGraph { |
| 229 | int index; |
| 230 | const char *graph_desc; |
| 231 | |
| 232 | AVFilterGraph *graph; |
| 233 | int reconfiguration; |
| 234 | |
| 235 | InputFilter **inputs; |
| 236 | int nb_inputs; |
| 237 | OutputFilter **outputs; |
| 238 | int nb_outputs; |
| 239 | } FilterGraph; |
| 240 | |
| 241 | typedef struct InputStream { |
| 242 | int file_index; |
| 243 | AVStream *st; |
| 244 | int discard; /* true if stream data should be discarded */ |
| 245 | int user_set_discard; |
| 246 | int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */ |
| 247 | #define DECODING_FOR_OST 1 |
| 248 | #define DECODING_FOR_FILTER 2 |
| 249 | |
| 250 | AVCodecContext *dec_ctx; |
| 251 | AVCodec *dec; |
| 252 | AVFrame *decoded_frame; |
| 253 | AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */ |
| 254 | |
| 255 | int64_t start; /* time when read started */ |
| 256 | /* predicted dts of the next packet read for this stream or (when there are |
| 257 | * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */ |
| 258 | int64_t next_dts; |
| 259 | int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units) |
| 260 | |
| 261 | int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units) |
| 262 | int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units) |
| 263 | int wrap_correction_done; |
| 264 | |
| 265 | int64_t filter_in_rescale_delta_last; |
| 266 | |
| 267 | double ts_scale; |
| 268 | int saw_first_ts; |
| 269 | int showed_multi_packet_warning; |
| 270 | AVDictionary *decoder_opts; |
| 271 | AVRational framerate; /* framerate forced with -r */ |
| 272 | int top_field_first; |
| 273 | int guess_layout_max; |
| 274 | |
| 275 | int resample_height; |
| 276 | int resample_width; |
| 277 | int resample_pix_fmt; |
| 278 | |
| 279 | int resample_sample_fmt; |
| 280 | int resample_sample_rate; |
| 281 | int resample_channels; |
| 282 | uint64_t resample_channel_layout; |
| 283 | |
| 284 | int fix_sub_duration; |
| 285 | struct { /* previous decoded subtitle and related variables */ |
| 286 | int got_output; |
| 287 | int ret; |
| 288 | AVSubtitle subtitle; |
| 289 | } prev_sub; |
| 290 | |
| 291 | struct sub2video { |
| 292 | int64_t last_pts; |
| 293 | int64_t end_pts; |
| 294 | AVFrame *frame; |
| 295 | int w, h; |
| 296 | } sub2video; |
| 297 | |
| 298 | int dr1; |
| 299 | |
| 300 | /* decoded data from this stream goes into all those filters |
| 301 | * currently video and audio only */ |
| 302 | InputFilter **filters; |
| 303 | int nb_filters; |
| 304 | |
| 305 | int reinit_filters; |
| 306 | |
| 307 | /* hwaccel options */ |
| 308 | enum HWAccelID hwaccel_id; |
| 309 | char *hwaccel_device; |
| 310 | |
| 311 | /* hwaccel context */ |
| 312 | enum HWAccelID active_hwaccel_id; |
| 313 | void *hwaccel_ctx; |
| 314 | void (*hwaccel_uninit)(AVCodecContext *s); |
| 315 | int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags); |
| 316 | int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame); |
| 317 | enum AVPixelFormat hwaccel_pix_fmt; |
| 318 | enum AVPixelFormat hwaccel_retrieved_pix_fmt; |
| 319 | |
| 320 | /* stats */ |
| 321 | // combined size of all the packets read |
| 322 | uint64_t data_size; |
| 323 | /* number of packets successfully read for this stream */ |
| 324 | uint64_t nb_packets; |
| 325 | // number of frames/samples retrieved from the decoder |
| 326 | uint64_t frames_decoded; |
| 327 | uint64_t samples_decoded; |
| 328 | } InputStream; |
| 329 | |
| 330 | typedef struct InputFile { |
| 331 | AVFormatContext *ctx; |
| 332 | int eof_reached; /* true if eof reached */ |
| 333 | int eagain; /* true if last read attempt returned EAGAIN */ |
| 334 | int ist_index; /* index of first stream in input_streams */ |
| 335 | int64_t input_ts_offset; |
| 336 | int64_t ts_offset; |
| 337 | int64_t last_ts; |
| 338 | int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */ |
| 339 | int64_t recording_time; |
| 340 | int nb_streams; /* number of stream that ffmpeg is aware of; may be different |
| 341 | from ctx.nb_streams if new streams appear during av_read_frame() */ |
| 342 | int nb_streams_warn; /* number of streams that the user was warned of */ |
| 343 | int rate_emu; |
| 344 | int accurate_seek; |
| 345 | |
| 346 | #if HAVE_PTHREADS |
| 347 | AVThreadMessageQueue *in_thread_queue; |
| 348 | pthread_t thread; /* thread reading from this file */ |
| 349 | int non_blocking; /* reading packets from the thread should not block */ |
| 350 | int joined; /* the thread has been joined */ |
| 351 | #endif |
| 352 | } InputFile; |
| 353 | |
| 354 | enum forced_keyframes_const { |
| 355 | FKF_N, |
| 356 | FKF_N_FORCED, |
| 357 | FKF_PREV_FORCED_N, |
| 358 | FKF_PREV_FORCED_T, |
| 359 | FKF_T, |
| 360 | FKF_NB |
| 361 | }; |
| 362 | |
| 363 | extern const char *const forced_keyframes_const_names[]; |
| 364 | |
| 365 | typedef enum { |
| 366 | ENCODER_FINISHED = 1, |
| 367 | MUXER_FINISHED = 2, |
| 368 | } OSTFinished ; |
| 369 | |
| 370 | typedef struct OutputStream { |
| 371 | int file_index; /* file index */ |
| 372 | int index; /* stream index in the output file */ |
| 373 | int source_index; /* InputStream index */ |
| 374 | AVStream *st; /* stream in the output file */ |
| 375 | int encoding_needed; /* true if encoding needed for this stream */ |
| 376 | int frame_number; |
| 377 | /* input pts and corresponding output pts |
| 378 | for A/V sync */ |
| 379 | struct InputStream *sync_ist; /* input stream to sync against */ |
| 380 | int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number |
| 381 | /* pts of the first frame encoded for this stream, used for limiting |
| 382 | * recording time */ |
| 383 | int64_t first_pts; |
| 384 | /* dts of the last packet sent to the muxer */ |
| 385 | int64_t last_mux_dts; |
| 386 | AVBitStreamFilterContext *bitstream_filters; |
| 387 | AVCodecContext *enc_ctx; |
| 388 | AVCodec *enc; |
| 389 | int64_t max_frames; |
| 390 | AVFrame *filtered_frame; |
| 391 | AVFrame *last_frame; |
| 392 | int last_droped; |
| 393 | |
| 394 | /* video only */ |
| 395 | AVRational frame_rate; |
| 396 | int force_fps; |
| 397 | int top_field_first; |
| 398 | |
| 399 | AVRational frame_aspect_ratio; |
| 400 | |
| 401 | /* forced key frames */ |
| 402 | int64_t *forced_kf_pts; |
| 403 | int forced_kf_count; |
| 404 | int forced_kf_index; |
| 405 | char *forced_keyframes; |
| 406 | AVExpr *forced_keyframes_pexpr; |
| 407 | double forced_keyframes_expr_const_values[FKF_NB]; |
| 408 | |
| 409 | /* audio only */ |
| 410 | int *audio_channels_map; /* list of the channels id to pick from the source stream */ |
| 411 | int audio_channels_mapped; /* number of channels in audio_channels_map */ |
| 412 | |
| 413 | char *logfile_prefix; |
| 414 | FILE *logfile; |
| 415 | |
| 416 | OutputFilter *filter; |
| 417 | char *avfilter; |
| 418 | char *filters; ///< filtergraph associated to the -filter option |
| 419 | char *filters_script; ///< filtergraph script associated to the -filter_script option |
| 420 | |
| 421 | int64_t sws_flags; |
| 422 | AVDictionary *encoder_opts; |
| 423 | AVDictionary *swr_opts; |
| 424 | AVDictionary *resample_opts; |
| 425 | AVDictionary *bsf_args; |
| 426 | char *apad; |
| 427 | OSTFinished finished; /* no more packets should be written for this stream */ |
| 428 | int unavailable; /* true if the steram is unavailable (possibly temporarily) */ |
| 429 | int stream_copy; |
| 430 | const char *attachment_filename; |
| 431 | int copy_initial_nonkeyframes; |
| 432 | int copy_prior_start; |
| 433 | |
| 434 | int keep_pix_fmt; |
| 435 | |
| 436 | AVCodecParserContext *parser; |
| 437 | |
| 438 | /* stats */ |
| 439 | // combined size of all the packets written |
| 440 | uint64_t data_size; |
| 441 | // number of packets send to the muxer |
| 442 | uint64_t packets_written; |
| 443 | // number of frames/samples sent to the encoder |
| 444 | uint64_t frames_encoded; |
| 445 | uint64_t samples_encoded; |
| 446 | } OutputStream; |
| 447 | |
| 448 | typedef struct OutputFile { |
| 449 | AVFormatContext *ctx; |
| 450 | AVDictionary *opts; |
| 451 | int ost_index; /* index of the first stream in output_streams */ |
| 452 | int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units |
| 453 | int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units |
| 454 | uint64_t limit_filesize; /* filesize limit expressed in bytes */ |
| 455 | |
| 456 | int shortest; |
| 457 | } OutputFile; |
| 458 | |
| 459 | extern InputStream **input_streams; |
| 460 | extern int nb_input_streams; |
| 461 | extern InputFile **input_files; |
| 462 | extern int nb_input_files; |
| 463 | |
| 464 | extern OutputStream **output_streams; |
| 465 | extern int nb_output_streams; |
| 466 | extern OutputFile **output_files; |
| 467 | extern int nb_output_files; |
| 468 | |
| 469 | extern FilterGraph **filtergraphs; |
| 470 | extern int nb_filtergraphs; |
| 471 | |
| 472 | extern char *vstats_filename; |
| 473 | |
| 474 | extern float audio_drift_threshold; |
| 475 | extern float dts_delta_threshold; |
| 476 | extern float dts_error_threshold; |
| 477 | |
| 478 | extern int audio_volume; |
| 479 | extern int audio_sync_method; |
| 480 | extern int video_sync_method; |
| 481 | extern int do_benchmark; |
| 482 | extern int do_benchmark_all; |
| 483 | extern int do_deinterlace; |
| 484 | extern int do_hex_dump; |
| 485 | extern int do_pkt_dump; |
| 486 | extern int copy_ts; |
| 487 | extern int start_at_zero; |
| 488 | extern int copy_tb; |
| 489 | extern int debug_ts; |
| 490 | extern int exit_on_error; |
| 491 | extern int print_stats; |
| 492 | extern int qp_hist; |
| 493 | extern int stdin_interaction; |
| 494 | extern int frame_bits_per_raw_sample; |
| 495 | extern AVIOContext *progress_avio; |
| 496 | extern float max_error_rate; |
| 497 | extern int vdpau_api_ver; |
| 498 | |
| 499 | extern const AVIOInterruptCB int_cb; |
| 500 | |
| 501 | extern const OptionDef options[]; |
| 502 | extern const HWAccel hwaccels[]; |
| 503 | |
| 504 | |
| 505 | void term_init(void); |
| 506 | void term_exit(void); |
| 507 | |
| 508 | void reset_options(OptionsContext *o, int is_input); |
| 509 | void show_usage(void); |
| 510 | |
| 511 | void opt_output_file(void *optctx, const char *filename); |
| 512 | |
| 513 | void remove_avoptions(AVDictionary **a, AVDictionary *b); |
| 514 | void assert_avoptions(AVDictionary *m); |
| 515 | |
| 516 | int guess_input_channel_layout(InputStream *ist); |
| 517 | |
| 518 | enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target); |
| 519 | void choose_sample_fmt(AVStream *st, AVCodec *codec); |
| 520 | |
| 521 | int configure_filtergraph(FilterGraph *fg); |
| 522 | int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out); |
| 523 | int ist_in_filtergraph(FilterGraph *fg, InputStream *ist); |
| 524 | FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost); |
| 525 | |
| 526 | int ffmpeg_parse_options(int argc, char **argv); |
| 527 | |
| 528 | int vdpau_init(AVCodecContext *s); |
| 529 | int dxva2_init(AVCodecContext *s); |
| 530 | int vda_init(AVCodecContext *s); |
| 531 | |
| 532 | #endif /* FFMPEG_H */ |