Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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