ffmpeg.h 19.6 KB
Newer Older
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5 6 7 8
 * 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.
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10 11 12 13 14
 * 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
15
 * License along with FFmpeg; if not, write to the Free Software
16 17 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

James Almer's avatar
James Almer committed
19 20
#ifndef FFTOOLS_FFMPEG_H
#define FFTOOLS_FFMPEG_H
21 22 23 24 25

#include "config.h"

#include <stdint.h>
#include <stdio.h>
26
#include <signal.h>
27 28 29 30 31 32 33 34 35 36 37 38

#include "cmdutils.h"

#include "libavformat/avformat.h"
#include "libavformat/avio.h"

#include "libavcodec/avcodec.h"

#include "libavfilter/avfilter.h"

#include "libavutil/avutil.h"
#include "libavutil/dict.h"
39
#include "libavutil/eval.h"
40
#include "libavutil/fifo.h"
41
#include "libavutil/hwcontext.h"
42 43
#include "libavutil/pixfmt.h"
#include "libavutil/rational.h"
44
#include "libavutil/thread.h"
45
#include "libavutil/threadmessage.h"
46

47 48
#include "libswresample/swresample.h"

49 50 51 52
#define VSYNC_AUTO       -1
#define VSYNC_PASSTHROUGH 0
#define VSYNC_CFR         1
#define VSYNC_VFR         2
53
#define VSYNC_VSCFR       0xfe
54 55 56
#define VSYNC_DROP        0xff

#define MAX_STREAMS 1024    /* arbitrary sanity check value */
57

58 59 60
enum HWAccelID {
    HWACCEL_NONE = 0,
    HWACCEL_AUTO,
61
    HWACCEL_GENERIC,
62
    HWACCEL_VIDEOTOOLBOX,
63
    HWACCEL_QSV,
64
    HWACCEL_CUVID,
65 66 67 68 69 70 71 72 73
};

typedef struct HWAccel {
    const char *name;
    int (*init)(AVCodecContext *s);
    enum HWAccelID id;
    enum AVPixelFormat pix_fmt;
} HWAccel;

74
typedef struct HWDevice {
75
    const char *name;
76 77 78 79
    enum AVHWDeviceType type;
    AVBufferRef *device_ref;
} HWDevice;

80 81
/* select an input stream for an output stream */
typedef struct StreamMap {
82
    int disabled;           /* 1 is this mapping is disabled by a negative map */
83 84 85 86
    int file_index;
    int stream_index;
    int sync_file_index;
    int sync_stream_index;
87
    char *linklabel;       /* name of an output link, for mapping lavfi outputs */
88 89
} StreamMap;

90 91 92 93
typedef struct {
    int  file_idx,  stream_idx,  channel_idx; // input
    int ofile_idx, ostream_idx;               // output
} AudioChannelMap;
94 95

typedef struct OptionsContext {
96 97
    OptionGroup *g;

98 99
    /* input/output options */
    int64_t start_time;
100
    int64_t start_time_eof;
101
    int seek_timestamp;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    const char *format;

    SpecifierOpt *codec_names;
    int        nb_codec_names;
    SpecifierOpt *audio_channels;
    int        nb_audio_channels;
    SpecifierOpt *audio_sample_rate;
    int        nb_audio_sample_rate;
    SpecifierOpt *frame_rates;
    int        nb_frame_rates;
    SpecifierOpt *frame_sizes;
    int        nb_frame_sizes;
    SpecifierOpt *frame_pix_fmts;
    int        nb_frame_pix_fmts;

    /* input options */
    int64_t input_ts_offset;
119
    int loop;
120
    int rate_emu;
121
    int accurate_seek;
122
    int thread_queue_size;
123 124 125 126 127

    SpecifierOpt *ts_scale;
    int        nb_ts_scale;
    SpecifierOpt *dump_attachment;
    int        nb_dump_attachment;
128 129 130 131
    SpecifierOpt *hwaccels;
    int        nb_hwaccels;
    SpecifierOpt *hwaccel_devices;
    int        nb_hwaccel_devices;
132 133
    SpecifierOpt *hwaccel_output_formats;
    int        nb_hwaccel_output_formats;
134 135
    SpecifierOpt *autorotate;
    int        nb_autorotate;
136 137 138 139

    /* output options */
    StreamMap *stream_maps;
    int     nb_stream_maps;
140 141
    AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
    int           nb_audio_channel_maps; /* number of (valid) -map_channel settings */
142 143 144 145 146 147 148 149 150
    int metadata_global_manual;
    int metadata_streams_manual;
    int metadata_chapters_manual;
    const char **attachments;
    int       nb_attachments;

    int chapters_input_file;

    int64_t recording_time;
151
    int64_t stop_time;
152 153 154
    uint64_t limit_filesize;
    float mux_preload;
    float mux_max_delay;
155
    int shortest;
156
    int bitexact;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

    int video_disable;
    int audio_disable;
    int subtitle_disable;
    int data_disable;

    /* indexed by output file stream index */
    int   *streamid_map;
    int nb_streamid_map;

    SpecifierOpt *metadata;
    int        nb_metadata;
    SpecifierOpt *max_frames;
    int        nb_max_frames;
    SpecifierOpt *bitstream_filters;
    int        nb_bitstream_filters;
    SpecifierOpt *codec_tags;
    int        nb_codec_tags;
    SpecifierOpt *sample_fmts;
    int        nb_sample_fmts;
    SpecifierOpt *qscale;
    int        nb_qscale;
    SpecifierOpt *forced_key_frames;
    int        nb_forced_key_frames;
    SpecifierOpt *force_fps;
    int        nb_force_fps;
    SpecifierOpt *frame_aspect_ratios;
    int        nb_frame_aspect_ratios;
    SpecifierOpt *rc_overrides;
    int        nb_rc_overrides;
    SpecifierOpt *intra_matrices;
    int        nb_intra_matrices;
    SpecifierOpt *inter_matrices;
    int        nb_inter_matrices;
191 192
    SpecifierOpt *chroma_intra_matrices;
    int        nb_chroma_intra_matrices;
193 194 195 196 197 198 199 200
    SpecifierOpt *top_field_first;
    int        nb_top_field_first;
    SpecifierOpt *metadata_map;
    int        nb_metadata_map;
    SpecifierOpt *presets;
    int        nb_presets;
    SpecifierOpt *copy_initial_nonkeyframes;
    int        nb_copy_initial_nonkeyframes;
201 202
    SpecifierOpt *copy_prior_start;
    int        nb_copy_prior_start;
203 204
    SpecifierOpt *filters;
    int        nb_filters;
205 206
    SpecifierOpt *filter_scripts;
    int        nb_filter_scripts;
207 208
    SpecifierOpt *reinit_filters;
    int        nb_reinit_filters;
209 210
    SpecifierOpt *fix_sub_duration;
    int        nb_fix_sub_duration;
211 212
    SpecifierOpt *canvas_sizes;
    int        nb_canvas_sizes;
213 214
    SpecifierOpt *pass;
    int        nb_pass;
215 216
    SpecifierOpt *passlogfiles;
    int        nb_passlogfiles;
217 218
    SpecifierOpt *max_muxing_queue_size;
    int        nb_max_muxing_queue_size;
219 220
    SpecifierOpt *guess_layout_max;
    int        nb_guess_layout_max;
221 222
    SpecifierOpt *apad;
    int        nb_apad;
223 224
    SpecifierOpt *discard;
    int        nb_discard;
225 226
    SpecifierOpt *disposition;
    int        nb_disposition;
227 228
    SpecifierOpt *program;
    int        nb_program;
229 230
    SpecifierOpt *time_bases;
    int        nb_time_bases;
231 232
    SpecifierOpt *enc_time_bases;
    int        nb_enc_time_bases;
233 234 235 236 237 238 239
} OptionsContext;

typedef struct InputFilter {
    AVFilterContext    *filter;
    struct InputStream *ist;
    struct FilterGraph *graph;
    uint8_t            *name;
240 241 242
    enum AVMediaType    type;   // AVMEDIA_TYPE_SUBTITLE for sub2video

    AVFifoBuffer *frame_queue;
243 244 245 246 247 248 249 250

    // parameters configured for this input
    int format;

    int width, height;
    AVRational sample_aspect_ratio;

    int sample_rate;
251
    int channels;
252 253 254
    uint64_t channel_layout;

    AVBufferRef *hw_frames_ctx;
255 256

    int eof;
257 258 259 260 261 262 263 264 265 266
} InputFilter;

typedef struct OutputFilter {
    AVFilterContext     *filter;
    struct OutputStream *ost;
    struct FilterGraph  *graph;
    uint8_t             *name;

    /* temporary storage until stream maps are processed */
    AVFilterInOut       *out_tmp;
267
    enum AVMediaType     type;
268 269 270 271 272 273 274 275 276 277 278 279

    /* desired output stream properties */
    int width, height;
    AVRational frame_rate;
    int format;
    int sample_rate;
    uint64_t channel_layout;

    // those are only set if no format is specified and the encoder gives us multiple options
    int *formats;
    uint64_t *channel_layouts;
    int *sample_rates;
280 281 282 283 284 285 286
} OutputFilter;

typedef struct FilterGraph {
    int            index;
    const char    *graph_desc;

    AVFilterGraph *graph;
287
    int reconfiguration;
288 289 290 291 292 293 294 295 296 297 298

    InputFilter   **inputs;
    int          nb_inputs;
    OutputFilter **outputs;
    int         nb_outputs;
} FilterGraph;

typedef struct InputStream {
    int file_index;
    AVStream *st;
    int discard;             /* true if stream data should be discarded */
299
    int user_set_discard;
300 301 302 303
    int decoding_needed;     /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
#define DECODING_FOR_OST    1
#define DECODING_FOR_FILTER 2

304
    AVCodecContext *dec_ctx;
305 306
    AVCodec *dec;
    AVFrame *decoded_frame;
307
    AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
308 309 310

    int64_t       start;     /* time when read started */
    /* predicted dts of the next packet read for this stream or (when there are
311
     * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
312
    int64_t       next_dts;
313 314 315 316 317
    int64_t       dts;       ///< dts of the last packet read for this stream (in AV_TIME_BASE units)

    int64_t       next_pts;  ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
    int64_t       pts;       ///< current pts of the decoded frame  (in AV_TIME_BASE units)
    int           wrap_correction_done;
318 319

    int64_t filter_in_rescale_delta_last;
320

321 322
    int64_t min_pts; /* pts with the smallest value in a current stream */
    int64_t max_pts; /* pts with the higher value in a current stream */
323 324 325 326 327

    // when forcing constant input framerate through -r,
    // this contains the pts that will be given to the next decoded frame
    int64_t cfr_next_pts;

328
    int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
329

330
    double ts_scale;
331
    int saw_first_ts;
332
    AVDictionary *decoder_opts;
333
    AVRational framerate;               /* framerate forced with -r */
334
    int top_field_first;
335
    int guess_layout_max;
336

337
    int autorotate;
338

339 340 341 342 343 344 345
    int fix_sub_duration;
    struct { /* previous decoded subtitle and related variables */
        int got_output;
        int ret;
        AVSubtitle subtitle;
    } prev_sub;

346 347
    struct sub2video {
        int64_t last_pts;
348
        int64_t end_pts;
349
        AVFifoBuffer *sub_queue;    ///< queue of AVSubtitle* before filter init
350
        AVFrame *frame;
351 352 353 354
        int w, h;
    } sub2video;

    int dr1;
355 356 357 358 359

    /* decoded data from this stream goes into all those filters
     * currently video and audio only */
    InputFilter **filters;
    int        nb_filters;
360 361

    int reinit_filters;
362

363 364
    /* hwaccel options */
    enum HWAccelID hwaccel_id;
365
    enum AVHWDeviceType hwaccel_device_type;
366
    char  *hwaccel_device;
367
    enum AVPixelFormat hwaccel_output_format;
368 369 370 371 372 373 374 375

    /* hwaccel context */
    void  *hwaccel_ctx;
    void (*hwaccel_uninit)(AVCodecContext *s);
    int  (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
    int  (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
    enum AVPixelFormat hwaccel_pix_fmt;
    enum AVPixelFormat hwaccel_retrieved_pix_fmt;
376
    AVBufferRef *hw_frames_ctx;
377 378 379 380 381 382 383 384 385

    /* stats */
    // combined size of all the packets read
    uint64_t data_size;
    /* number of packets successfully read for this stream */
    uint64_t nb_packets;
    // number of frames/samples retrieved from the decoder
    uint64_t frames_decoded;
    uint64_t samples_decoded;
wm4's avatar
wm4 committed
386 387 388

    int64_t *dts_buffer;
    int nb_dts_buffer;
389 390

    int got_output;
391 392 393 394 395
} InputStream;

typedef struct InputFile {
    AVFormatContext *ctx;
    int eof_reached;      /* true if eof reached */
396
    int eagain;           /* true if last read attempt returned EAGAIN */
397
    int ist_index;        /* index of first stream in input_streams */
398 399 400 401
    int loop;             /* set number of times input stream should be looped */
    int64_t duration;     /* actual duration of the longest stream in a file
                             at the moment when looping happens */
    AVRational time_base; /* time base of the duration */
402
    int64_t input_ts_offset;
403

404
    int64_t ts_offset;
405
    int64_t last_ts;
406
    int64_t start_time;   /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
407
    int seek_timestamp;
408
    int64_t recording_time;
409
    int nb_streams;       /* number of stream that ffmpeg is aware of; may be different
410
                             from ctx.nb_streams if new streams appear during av_read_frame() */
411
    int nb_streams_warn;  /* number of streams that the user was warned of */
412
    int rate_emu;
413
    int accurate_seek;
414

415
#if HAVE_THREADS
416
    AVThreadMessageQueue *in_thread_queue;
417
    pthread_t thread;           /* thread reading from this file */
418
    int non_blocking;           /* reading packets from the thread should not block */
419
    int joined;                 /* the thread has been joined */
420
    int thread_queue_size;      /* maximum number of queued packets */
421 422 423
#endif
} InputFile;

424 425 426 427 428 429 430 431 432
enum forced_keyframes_const {
    FKF_N,
    FKF_N_FORCED,
    FKF_PREV_FORCED_N,
    FKF_PREV_FORCED_T,
    FKF_T,
    FKF_NB
};

433 434
#define ABORT_ON_FLAG_EMPTY_OUTPUT (1 <<  0)

435 436
extern const char *const forced_keyframes_const_names[];

437 438 439 440 441
typedef enum {
    ENCODER_FINISHED = 1,
    MUXER_FINISHED = 2,
} OSTFinished ;

442 443 444 445 446 447 448 449 450 451 452 453 454 455
typedef struct OutputStream {
    int file_index;          /* file index */
    int index;               /* stream index in the output file */
    int source_index;        /* InputStream index */
    AVStream *st;            /* stream in the output file */
    int encoding_needed;     /* true if encoding needed for this stream */
    int frame_number;
    /* input pts and corresponding output pts
       for A/V sync */
    struct InputStream *sync_ist; /* input stream to sync against */
    int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
    /* pts of the first frame encoded for this stream, used for limiting
     * recording time */
    int64_t first_pts;
456 457
    /* dts of the last packet sent to the muxer */
    int64_t last_mux_dts;
458 459
    // the timebase of the packets sent to the muxer
    AVRational mux_timebase;
460
    AVRational enc_timebase;
461 462 463 464

    int                    nb_bitstream_filters;
    AVBSFContext            **bsf_ctx;

465
    AVCodecContext *enc_ctx;
466
    AVCodecParameters *ref_par; /* associated input codec parameters with encoders options applied */
467 468 469
    AVCodec *enc;
    int64_t max_frames;
    AVFrame *filtered_frame;
470
    AVFrame *last_frame;
471
    int last_dropped;
472
    int last_nb0_frames[3];
473

474 475
    void  *hwaccel_ctx;

476 477
    /* video only */
    AVRational frame_rate;
478
    int is_cfr;
479 480
    int force_fps;
    int top_field_first;
481
    int rotate_overridden;
482
    double rotate_override_value;
483

484
    AVRational frame_aspect_ratio;
485 486

    /* forced key frames */
487
    int64_t forced_kf_ref_pts;
488 489 490 491
    int64_t *forced_kf_pts;
    int forced_kf_count;
    int forced_kf_index;
    char *forced_keyframes;
492 493
    AVExpr *forced_keyframes_pexpr;
    double forced_keyframes_expr_const_values[FKF_NB];
494

495
    /* audio only */
496
    int *audio_channels_map;             /* list of the channels id to pick from the source stream */
497 498
    int audio_channels_mapped;           /* number of channels in audio_channels_map */

499
    char *logfile_prefix;
500 501 502 503
    FILE *logfile;

    OutputFilter *filter;
    char *avfilter;
504 505
    char *filters;         ///< filtergraph associated to the -filter option
    char *filters_script;  ///< filtergraph script associated to the -filter_script option
506

507
    AVDictionary *encoder_opts;
508
    AVDictionary *sws_dict;
509
    AVDictionary *swr_opts;
510
    AVDictionary *resample_opts;
511
    char *apad;
512
    OSTFinished finished;        /* no more packets should be written for this stream */
513
    int unavailable;                     /* true if the steram is unavailable (possibly temporarily) */
514
    int stream_copy;
515 516

    // init_output_stream() has been called for this stream
517
    // The encoder and the bitstream filters have been initialized and the stream
518 519 520
    // parameters are set in the AVStream.
    int initialized;

521 522
    int inputs_done;

523 524
    const char *attachment_filename;
    int copy_initial_nonkeyframes;
525
    int copy_prior_start;
526
    char *disposition;
527

528
    int keep_pix_fmt;
529

530 531 532
    /* stats */
    // combined size of all the packets written
    uint64_t data_size;
533 534 535 536 537
    // number of packets send to the muxer
    uint64_t packets_written;
    // number of frames/samples sent to the encoder
    uint64_t frames_encoded;
    uint64_t samples_encoded;
538 539 540

    /* packet quality factor */
    int quality;
541

542 543 544 545
    int max_muxing_queue_size;

    /* the packets are buffered here until the muxer is ready to be initialized */
    AVFifoBuffer *muxing_queue;
546

547 548 549
    /* packet picture type */
    int pict_type;

550 551
    /* frame encode sum of squared error values */
    int64_t error[4];
552 553 554 555 556 557
} OutputStream;

typedef struct OutputFile {
    AVFormatContext *ctx;
    AVDictionary *opts;
    int ost_index;       /* index of the first stream in output_streams */
558 559 560
    int64_t recording_time;  ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
    int64_t start_time;      ///< start time in microseconds == AV_TIME_BASE units
    uint64_t limit_filesize; /* filesize limit expressed in bytes */
561 562

    int shortest;
563 564

    int header_written;
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
} OutputFile;

extern InputStream **input_streams;
extern int        nb_input_streams;
extern InputFile   **input_files;
extern int        nb_input_files;

extern OutputStream **output_streams;
extern int         nb_output_streams;
extern OutputFile   **output_files;
extern int         nb_output_files;

extern FilterGraph **filtergraphs;
extern int        nb_filtergraphs;

extern char *vstats_filename;
581
extern char *sdp_filename;
582 583 584

extern float audio_drift_threshold;
extern float dts_delta_threshold;
585
extern float dts_error_threshold;
586 587 588 589

extern int audio_volume;
extern int audio_sync_method;
extern int video_sync_method;
590
extern float frame_drop_threshold;
591
extern int do_benchmark;
592
extern int do_benchmark_all;
593 594 595 596
extern int do_deinterlace;
extern int do_hex_dump;
extern int do_pkt_dump;
extern int copy_ts;
597
extern int start_at_zero;
598
extern int copy_tb;
599
extern int debug_ts;
600
extern int exit_on_error;
601
extern int abort_on_flags;
602 603
extern int print_stats;
extern int qp_hist;
604 605 606
extern int stdin_interaction;
extern int frame_bits_per_raw_sample;
extern AVIOContext *progress_avio;
607
extern float max_error_rate;
608
extern char *videotoolbox_pixfmt;
609

610 611
extern int filter_nbthreads;
extern int filter_complex_nbthreads;
612
extern int vstats_version;
613

614 615 616
extern const AVIOInterruptCB int_cb;

extern const OptionDef options[];
617
extern const HWAccel hwaccels[];
618
extern AVBufferRef *hw_device_ctx;
619 620 621
#if CONFIG_QSV
extern char *qsv_device;
#endif
622
extern HWDevice *filter_hw_device;
623

624

625 626
void term_init(void);
void term_exit(void);
627

628 629
void reset_options(OptionsContext *o, int is_input);
void show_usage(void);
630 631 632

void opt_output_file(void *optctx, const char *filename);

633
void remove_avoptions(AVDictionary **a, AVDictionary *b);
634 635 636 637
void assert_avoptions(AVDictionary *m);

int guess_input_channel_layout(InputStream *ist);

638
enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *avctx, AVCodec *codec, enum AVPixelFormat target);
639 640
void choose_sample_fmt(AVStream *st, AVCodec *codec);

641 642
int configure_filtergraph(FilterGraph *fg);
int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
643
void check_filter_outputs(void);
644
int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
645
int filtergraph_is_simple(FilterGraph *fg);
646
int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
647
int init_complex_filtergraph(FilterGraph *fg);
648

649 650
void sub2video_update(InputStream *ist, AVSubtitle *sub);

651 652
int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);

653
int ffmpeg_parse_options(int argc, char **argv);
654

655
int videotoolbox_init(AVCodecContext *s);
656
int qsv_init(AVCodecContext *s);
657
int cuvid_init(AVCodecContext *s);
658

659 660 661 662 663 664 665 666 667
HWDevice *hw_device_get_by_name(const char *name);
int hw_device_init_from_string(const char *arg, HWDevice **dev);
void hw_device_free_all(void);

int hw_device_setup_for_decode(InputStream *ist);
int hw_device_setup_for_encode(OutputStream *ost);

int hwaccel_decode_init(AVCodecContext *avctx);

James Almer's avatar
James Almer committed
668
#endif /* FFTOOLS_FFMPEG_H */