ffmpeg.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. #ifndef FFTOOLS_FFMPEG_H
  19. #define FFTOOLS_FFMPEG_H
  20. #include "config.h"
  21. #include <stdatomic.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <signal.h>
  25. #include "cmdutils.h"
  26. #include "sync_queue.h"
  27. #include "libavformat/avformat.h"
  28. #include "libavformat/avio.h"
  29. #include "libavcodec/avcodec.h"
  30. #include "libavcodec/bsf.h"
  31. #include "libavfilter/avfilter.h"
  32. #include "libavutil/avutil.h"
  33. #include "libavutil/dict.h"
  34. #include "libavutil/eval.h"
  35. #include "libavutil/fifo.h"
  36. #include "libavutil/hwcontext.h"
  37. #include "libavutil/pixfmt.h"
  38. #include "libavutil/rational.h"
  39. #include "libavutil/thread.h"
  40. #include "libavutil/threadmessage.h"
  41. #include "libswresample/swresample.h"
  42. // deprecated features
  43. #define FFMPEG_OPT_PSNR 1
  44. #define FFMPEG_OPT_MAP_CHANNEL 1
  45. #define FFMPEG_OPT_MAP_SYNC 1
  46. #define FFMPEG_ROTATION_METADATA 1
  47. enum VideoSyncMethod {
  48. VSYNC_AUTO = -1,
  49. VSYNC_PASSTHROUGH,
  50. VSYNC_CFR,
  51. VSYNC_VFR,
  52. VSYNC_VSCFR,
  53. VSYNC_DROP,
  54. };
  55. #define MAX_STREAMS 1024 /* arbitrary sanity check value */
  56. enum HWAccelID {
  57. HWACCEL_NONE = 0,
  58. HWACCEL_AUTO,
  59. HWACCEL_GENERIC,
  60. };
  61. typedef struct HWDevice {
  62. const char *name;
  63. enum AVHWDeviceType type;
  64. AVBufferRef *device_ref;
  65. } HWDevice;
  66. /* select an input stream for an output stream */
  67. typedef struct StreamMap {
  68. int disabled; /* 1 is this mapping is disabled by a negative map */
  69. int file_index;
  70. int stream_index;
  71. char *linklabel; /* name of an output link, for mapping lavfi outputs */
  72. } StreamMap;
  73. #if FFMPEG_OPT_MAP_CHANNEL
  74. typedef struct {
  75. int file_idx, stream_idx, channel_idx; // input
  76. int ofile_idx, ostream_idx; // output
  77. } AudioChannelMap;
  78. #endif
  79. typedef struct OptionsContext {
  80. OptionGroup *g;
  81. /* input/output options */
  82. int64_t start_time;
  83. int64_t start_time_eof;
  84. int seek_timestamp;
  85. const char *format;
  86. SpecifierOpt *codec_names;
  87. int nb_codec_names;
  88. SpecifierOpt *audio_ch_layouts;
  89. int nb_audio_ch_layouts;
  90. SpecifierOpt *audio_channels;
  91. int nb_audio_channels;
  92. SpecifierOpt *audio_sample_rate;
  93. int nb_audio_sample_rate;
  94. SpecifierOpt *frame_rates;
  95. int nb_frame_rates;
  96. SpecifierOpt *max_frame_rates;
  97. int nb_max_frame_rates;
  98. SpecifierOpt *frame_sizes;
  99. int nb_frame_sizes;
  100. SpecifierOpt *frame_pix_fmts;
  101. int nb_frame_pix_fmts;
  102. /* input options */
  103. int64_t input_ts_offset;
  104. int loop;
  105. int rate_emu;
  106. float readrate;
  107. int accurate_seek;
  108. int thread_queue_size;
  109. int input_sync_ref;
  110. int find_stream_info;
  111. SpecifierOpt *ts_scale;
  112. int nb_ts_scale;
  113. SpecifierOpt *dump_attachment;
  114. int nb_dump_attachment;
  115. SpecifierOpt *hwaccels;
  116. int nb_hwaccels;
  117. SpecifierOpt *hwaccel_devices;
  118. int nb_hwaccel_devices;
  119. SpecifierOpt *hwaccel_output_formats;
  120. int nb_hwaccel_output_formats;
  121. SpecifierOpt *autorotate;
  122. int nb_autorotate;
  123. /* output options */
  124. StreamMap *stream_maps;
  125. int nb_stream_maps;
  126. #if FFMPEG_OPT_MAP_CHANNEL
  127. AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
  128. int nb_audio_channel_maps; /* number of (valid) -map_channel settings */
  129. #endif
  130. const char **attachments;
  131. int nb_attachments;
  132. int chapters_input_file;
  133. int64_t recording_time;
  134. int64_t stop_time;
  135. int64_t limit_filesize;
  136. float mux_preload;
  137. float mux_max_delay;
  138. float shortest_buf_duration;
  139. int shortest;
  140. int bitexact;
  141. int video_disable;
  142. int audio_disable;
  143. int subtitle_disable;
  144. int data_disable;
  145. /* indexed by output file stream index */
  146. int *streamid_map;
  147. int nb_streamid_map;
  148. SpecifierOpt *metadata;
  149. int nb_metadata;
  150. SpecifierOpt *max_frames;
  151. int nb_max_frames;
  152. SpecifierOpt *bitstream_filters;
  153. int nb_bitstream_filters;
  154. SpecifierOpt *codec_tags;
  155. int nb_codec_tags;
  156. SpecifierOpt *sample_fmts;
  157. int nb_sample_fmts;
  158. SpecifierOpt *qscale;
  159. int nb_qscale;
  160. SpecifierOpt *forced_key_frames;
  161. int nb_forced_key_frames;
  162. SpecifierOpt *fps_mode;
  163. int nb_fps_mode;
  164. SpecifierOpt *force_fps;
  165. int nb_force_fps;
  166. SpecifierOpt *frame_aspect_ratios;
  167. int nb_frame_aspect_ratios;
  168. SpecifierOpt *display_rotations;
  169. int nb_display_rotations;
  170. SpecifierOpt *display_hflips;
  171. int nb_display_hflips;
  172. SpecifierOpt *display_vflips;
  173. int nb_display_vflips;
  174. SpecifierOpt *rc_overrides;
  175. int nb_rc_overrides;
  176. SpecifierOpt *intra_matrices;
  177. int nb_intra_matrices;
  178. SpecifierOpt *inter_matrices;
  179. int nb_inter_matrices;
  180. SpecifierOpt *chroma_intra_matrices;
  181. int nb_chroma_intra_matrices;
  182. SpecifierOpt *top_field_first;
  183. int nb_top_field_first;
  184. SpecifierOpt *metadata_map;
  185. int nb_metadata_map;
  186. SpecifierOpt *presets;
  187. int nb_presets;
  188. SpecifierOpt *copy_initial_nonkeyframes;
  189. int nb_copy_initial_nonkeyframes;
  190. SpecifierOpt *copy_prior_start;
  191. int nb_copy_prior_start;
  192. SpecifierOpt *filters;
  193. int nb_filters;
  194. SpecifierOpt *filter_scripts;
  195. int nb_filter_scripts;
  196. SpecifierOpt *reinit_filters;
  197. int nb_reinit_filters;
  198. SpecifierOpt *fix_sub_duration;
  199. int nb_fix_sub_duration;
  200. SpecifierOpt *fix_sub_duration_heartbeat;
  201. int nb_fix_sub_duration_heartbeat;
  202. SpecifierOpt *canvas_sizes;
  203. int nb_canvas_sizes;
  204. SpecifierOpt *pass;
  205. int nb_pass;
  206. SpecifierOpt *passlogfiles;
  207. int nb_passlogfiles;
  208. SpecifierOpt *max_muxing_queue_size;
  209. int nb_max_muxing_queue_size;
  210. SpecifierOpt *muxing_queue_data_threshold;
  211. int nb_muxing_queue_data_threshold;
  212. SpecifierOpt *guess_layout_max;
  213. int nb_guess_layout_max;
  214. SpecifierOpt *apad;
  215. int nb_apad;
  216. SpecifierOpt *discard;
  217. int nb_discard;
  218. SpecifierOpt *disposition;
  219. int nb_disposition;
  220. SpecifierOpt *program;
  221. int nb_program;
  222. SpecifierOpt *time_bases;
  223. int nb_time_bases;
  224. SpecifierOpt *enc_time_bases;
  225. int nb_enc_time_bases;
  226. SpecifierOpt *autoscale;
  227. int nb_autoscale;
  228. SpecifierOpt *bits_per_raw_sample;
  229. int nb_bits_per_raw_sample;
  230. SpecifierOpt *enc_stats_pre;
  231. int nb_enc_stats_pre;
  232. SpecifierOpt *enc_stats_post;
  233. int nb_enc_stats_post;
  234. SpecifierOpt *mux_stats;
  235. int nb_mux_stats;
  236. SpecifierOpt *enc_stats_pre_fmt;
  237. int nb_enc_stats_pre_fmt;
  238. SpecifierOpt *enc_stats_post_fmt;
  239. int nb_enc_stats_post_fmt;
  240. SpecifierOpt *mux_stats_fmt;
  241. int nb_mux_stats_fmt;
  242. } OptionsContext;
  243. typedef struct InputFilter {
  244. AVFilterContext *filter;
  245. struct InputStream *ist;
  246. struct FilterGraph *graph;
  247. uint8_t *name;
  248. enum AVMediaType type; // AVMEDIA_TYPE_SUBTITLE for sub2video
  249. AVFifo *frame_queue;
  250. // parameters configured for this input
  251. int format;
  252. int width, height;
  253. AVRational sample_aspect_ratio;
  254. int sample_rate;
  255. AVChannelLayout ch_layout;
  256. AVBufferRef *hw_frames_ctx;
  257. int32_t *displaymatrix;
  258. int eof;
  259. } InputFilter;
  260. typedef struct OutputFilter {
  261. AVFilterContext *filter;
  262. struct OutputStream *ost;
  263. struct FilterGraph *graph;
  264. uint8_t *name;
  265. /* temporary storage until stream maps are processed */
  266. AVFilterInOut *out_tmp;
  267. enum AVMediaType type;
  268. /* desired output stream properties */
  269. int width, height;
  270. AVRational frame_rate;
  271. int format;
  272. int sample_rate;
  273. AVChannelLayout ch_layout;
  274. // those are only set if no format is specified and the encoder gives us multiple options
  275. // They point directly to the relevant lists of the encoder.
  276. const int *formats;
  277. const AVChannelLayout *ch_layouts;
  278. const int *sample_rates;
  279. } OutputFilter;
  280. typedef struct FilterGraph {
  281. int index;
  282. const char *graph_desc;
  283. AVFilterGraph *graph;
  284. int reconfiguration;
  285. // true when the filtergraph contains only meta filters
  286. // that do not modify the frame data
  287. int is_meta;
  288. InputFilter **inputs;
  289. int nb_inputs;
  290. OutputFilter **outputs;
  291. int nb_outputs;
  292. } FilterGraph;
  293. typedef struct InputStream {
  294. int file_index;
  295. AVStream *st;
  296. int discard; /* true if stream data should be discarded */
  297. int user_set_discard;
  298. int decoding_needed; /* non zero if the packets must be decoded in 'raw_fifo', see DECODING_FOR_* */
  299. #define DECODING_FOR_OST 1
  300. #define DECODING_FOR_FILTER 2
  301. int processing_needed; /* non zero if the packets must be processed */
  302. // should attach FrameData as opaque_ref after decoding
  303. int want_frame_data;
  304. /**
  305. * Codec parameters - to be used by the decoding/streamcopy code.
  306. * st->codecpar should not be accessed, because it may be modified
  307. * concurrently by the demuxing thread.
  308. */
  309. AVCodecParameters *par;
  310. AVCodecContext *dec_ctx;
  311. const AVCodec *dec;
  312. AVFrame *decoded_frame;
  313. AVPacket *pkt;
  314. AVRational framerate_guessed;
  315. int64_t prev_pkt_pts;
  316. int64_t start; /* time when read started */
  317. /* predicted dts of the next packet read for this stream or (when there are
  318. * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
  319. int64_t next_dts;
  320. int64_t first_dts; ///< dts of the first packet read for this stream (in AV_TIME_BASE units)
  321. int64_t dts; ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
  322. int64_t next_pts; ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
  323. int64_t pts; ///< current pts of the decoded frame (in AV_TIME_BASE units)
  324. int wrap_correction_done;
  325. // the value of AVCodecParserContext.repeat_pict from the AVStream parser
  326. // for the last packet returned from ifile_get_packet()
  327. // -1 if unknown
  328. // FIXME: this is a hack, the avstream parser should not be used
  329. int last_pkt_repeat_pict;
  330. int64_t filter_in_rescale_delta_last;
  331. int64_t min_pts; /* pts with the smallest value in a current stream */
  332. int64_t max_pts; /* pts with the higher value in a current stream */
  333. // when forcing constant input framerate through -r,
  334. // this contains the pts that will be given to the next decoded frame
  335. int64_t cfr_next_pts;
  336. int64_t nb_samples; /* number of samples in the last decoded audio frame before looping */
  337. double ts_scale;
  338. int saw_first_ts;
  339. AVDictionary *decoder_opts;
  340. AVRational framerate; /* framerate forced with -r */
  341. int top_field_first;
  342. int guess_layout_max;
  343. int autorotate;
  344. int fix_sub_duration;
  345. struct { /* previous decoded subtitle and related variables */
  346. int got_output;
  347. int ret;
  348. AVSubtitle subtitle;
  349. } prev_sub;
  350. struct sub2video {
  351. int64_t last_pts;
  352. int64_t end_pts;
  353. AVFifo *sub_queue; ///< queue of AVSubtitle* before filter init
  354. AVFrame *frame;
  355. int w, h;
  356. unsigned int initialize; ///< marks if sub2video_update should force an initialization
  357. } sub2video;
  358. /* decoded data from this stream goes into all those filters
  359. * currently video and audio only */
  360. InputFilter **filters;
  361. int nb_filters;
  362. int reinit_filters;
  363. /* hwaccel options */
  364. enum HWAccelID hwaccel_id;
  365. enum AVHWDeviceType hwaccel_device_type;
  366. char *hwaccel_device;
  367. enum AVPixelFormat hwaccel_output_format;
  368. int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
  369. enum AVPixelFormat hwaccel_pix_fmt;
  370. /* stats */
  371. // combined size of all the packets read
  372. uint64_t data_size;
  373. /* number of packets successfully read for this stream */
  374. uint64_t nb_packets;
  375. // number of frames/samples retrieved from the decoder
  376. uint64_t frames_decoded;
  377. uint64_t samples_decoded;
  378. int64_t *dts_buffer;
  379. int nb_dts_buffer;
  380. int got_output;
  381. } InputStream;
  382. typedef struct LastFrameDuration {
  383. int stream_idx;
  384. int64_t duration;
  385. } LastFrameDuration;
  386. typedef struct InputFile {
  387. int index;
  388. AVFormatContext *ctx;
  389. int eof_reached; /* true if eof reached */
  390. int eagain; /* true if last read attempt returned EAGAIN */
  391. int64_t input_ts_offset;
  392. int input_sync_ref;
  393. /**
  394. * Effective format start time based on enabled streams.
  395. */
  396. int64_t start_time_effective;
  397. int64_t ts_offset;
  398. /**
  399. * Extra timestamp offset added by discontinuity handling.
  400. */
  401. int64_t ts_offset_discont;
  402. int64_t last_ts;
  403. int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
  404. int64_t recording_time;
  405. /* streams that ffmpeg is aware of;
  406. * there may be extra streams in ctx that are not mapped to an InputStream
  407. * if new streams appear dynamically during demuxing */
  408. InputStream **streams;
  409. int nb_streams;
  410. int rate_emu;
  411. float readrate;
  412. int accurate_seek;
  413. /* when looping the input file, this queue is used by decoders to report
  414. * the last frame duration back to the demuxer thread */
  415. AVThreadMessageQueue *audio_duration_queue;
  416. int audio_duration_queue_size;
  417. } InputFile;
  418. enum forced_keyframes_const {
  419. FKF_N,
  420. FKF_N_FORCED,
  421. FKF_PREV_FORCED_N,
  422. FKF_PREV_FORCED_T,
  423. FKF_T,
  424. FKF_NB
  425. };
  426. #define ABORT_ON_FLAG_EMPTY_OUTPUT (1 << 0)
  427. #define ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM (1 << 1)
  428. enum EncStatsType {
  429. ENC_STATS_LITERAL = 0,
  430. ENC_STATS_FILE_IDX,
  431. ENC_STATS_STREAM_IDX,
  432. ENC_STATS_FRAME_NUM,
  433. ENC_STATS_FRAME_NUM_IN,
  434. ENC_STATS_TIMEBASE,
  435. ENC_STATS_TIMEBASE_IN,
  436. ENC_STATS_PTS,
  437. ENC_STATS_PTS_TIME,
  438. ENC_STATS_PTS_IN,
  439. ENC_STATS_PTS_TIME_IN,
  440. ENC_STATS_DTS,
  441. ENC_STATS_DTS_TIME,
  442. ENC_STATS_SAMPLE_NUM,
  443. ENC_STATS_NB_SAMPLES,
  444. ENC_STATS_PKT_SIZE,
  445. ENC_STATS_BITRATE,
  446. ENC_STATS_AVG_BITRATE,
  447. };
  448. typedef struct EncStatsComponent {
  449. enum EncStatsType type;
  450. uint8_t *str;
  451. size_t str_len;
  452. } EncStatsComponent;
  453. typedef struct EncStats {
  454. EncStatsComponent *components;
  455. int nb_components;
  456. AVIOContext *io;
  457. } EncStats;
  458. extern const char *const forced_keyframes_const_names[];
  459. typedef enum {
  460. ENCODER_FINISHED = 1,
  461. MUXER_FINISHED = 2,
  462. } OSTFinished ;
  463. enum {
  464. KF_FORCE_SOURCE = 1,
  465. KF_FORCE_SOURCE_NO_DROP = 2,
  466. };
  467. typedef struct KeyframeForceCtx {
  468. int type;
  469. int64_t ref_pts;
  470. // timestamps of the forced keyframes, in AV_TIME_BASE_Q
  471. int64_t *pts;
  472. int nb_pts;
  473. int index;
  474. AVExpr *pexpr;
  475. double expr_const_values[FKF_NB];
  476. int dropped_keyframe;
  477. } KeyframeForceCtx;
  478. typedef struct OutputStream {
  479. const AVClass *class;
  480. int file_index; /* file index */
  481. int index; /* stream index in the output file */
  482. /* input stream that is the source for this output stream;
  483. * may be NULL for streams with no well-defined source, e.g.
  484. * attachments or outputs from complex filtergraphs */
  485. InputStream *ist;
  486. AVStream *st; /* stream in the output file */
  487. /* number of frames emitted by the video-encoding sync code */
  488. int64_t vsync_frame_number;
  489. /* predicted pts of the next frame to be encoded
  490. * audio/video encoding only */
  491. int64_t next_pts;
  492. /* dts of the last packet sent to the muxing queue, in AV_TIME_BASE_Q */
  493. int64_t last_mux_dts;
  494. /* pts of the last frame received from the filters, in AV_TIME_BASE_Q */
  495. int64_t last_filter_pts;
  496. // timestamp from which the streamcopied streams should start,
  497. // in AV_TIME_BASE_Q;
  498. // everything before it should be discarded
  499. int64_t ts_copy_start;
  500. // the timebase of the packets sent to the muxer
  501. AVRational mux_timebase;
  502. AVRational enc_timebase;
  503. AVCodecContext *enc_ctx;
  504. AVFrame *filtered_frame;
  505. AVFrame *last_frame;
  506. AVFrame *sq_frame;
  507. AVPacket *pkt;
  508. int64_t last_dropped;
  509. int64_t last_nb0_frames[3];
  510. /* video only */
  511. AVRational frame_rate;
  512. AVRational max_frame_rate;
  513. enum VideoSyncMethod vsync_method;
  514. int is_cfr;
  515. int force_fps;
  516. int top_field_first;
  517. #if FFMPEG_ROTATION_METADATA
  518. int rotate_overridden;
  519. #endif
  520. int autoscale;
  521. int bitexact;
  522. int bits_per_raw_sample;
  523. #if FFMPEG_ROTATION_METADATA
  524. double rotate_override_value;
  525. #endif
  526. AVRational frame_aspect_ratio;
  527. KeyframeForceCtx kf;
  528. /* audio only */
  529. #if FFMPEG_OPT_MAP_CHANNEL
  530. int *audio_channels_map; /* list of the channels id to pick from the source stream */
  531. int audio_channels_mapped; /* number of channels in audio_channels_map */
  532. #endif
  533. char *logfile_prefix;
  534. FILE *logfile;
  535. OutputFilter *filter;
  536. char *avfilter;
  537. char *filters; ///< filtergraph associated to the -filter option
  538. char *filters_script; ///< filtergraph script associated to the -filter_script option
  539. AVDictionary *encoder_opts;
  540. AVDictionary *sws_dict;
  541. AVDictionary *swr_opts;
  542. char *apad;
  543. OSTFinished finished; /* no more packets should be written for this stream */
  544. int unavailable; /* true if the steram is unavailable (possibly temporarily) */
  545. // init_output_stream() has been called for this stream
  546. // The encoder and the bitstream filters have been initialized and the stream
  547. // parameters are set in the AVStream.
  548. int initialized;
  549. int inputs_done;
  550. const char *attachment_filename;
  551. int streamcopy_started;
  552. int copy_initial_nonkeyframes;
  553. int copy_prior_start;
  554. int keep_pix_fmt;
  555. /* stats */
  556. // combined size of all the packets sent to the muxer
  557. uint64_t data_size_mux;
  558. // combined size of all the packets received from the encoder
  559. uint64_t data_size_enc;
  560. // number of packets send to the muxer
  561. atomic_uint_least64_t packets_written;
  562. // number of frames/samples sent to the encoder
  563. uint64_t frames_encoded;
  564. uint64_t samples_encoded;
  565. // number of packets received from the encoder
  566. uint64_t packets_encoded;
  567. /* packet quality factor */
  568. int quality;
  569. /* packet picture type */
  570. int pict_type;
  571. /* frame encode sum of squared error values */
  572. int64_t error[4];
  573. int sq_idx_encode;
  574. int sq_idx_mux;
  575. EncStats enc_stats_pre;
  576. EncStats enc_stats_post;
  577. /*
  578. * bool on whether this stream should be utilized for splitting
  579. * subtitles utilizing fix_sub_duration at random access points.
  580. */
  581. unsigned int fix_sub_duration_heartbeat;
  582. } OutputStream;
  583. typedef struct OutputFile {
  584. const AVClass *class;
  585. int index;
  586. const AVOutputFormat *format;
  587. const char *url;
  588. OutputStream **streams;
  589. int nb_streams;
  590. SyncQueue *sq_encode;
  591. int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
  592. int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
  593. int shortest;
  594. int bitexact;
  595. } OutputFile;
  596. extern InputFile **input_files;
  597. extern int nb_input_files;
  598. extern OutputFile **output_files;
  599. extern int nb_output_files;
  600. extern FilterGraph **filtergraphs;
  601. extern int nb_filtergraphs;
  602. extern char *vstats_filename;
  603. extern char *sdp_filename;
  604. extern float audio_drift_threshold;
  605. extern float dts_delta_threshold;
  606. extern float dts_error_threshold;
  607. extern enum VideoSyncMethod video_sync_method;
  608. extern float frame_drop_threshold;
  609. extern int do_benchmark;
  610. extern int do_benchmark_all;
  611. extern int do_hex_dump;
  612. extern int do_pkt_dump;
  613. extern int copy_ts;
  614. extern int start_at_zero;
  615. extern int copy_tb;
  616. extern int debug_ts;
  617. extern int exit_on_error;
  618. extern int abort_on_flags;
  619. extern int print_stats;
  620. extern int64_t stats_period;
  621. extern int qp_hist;
  622. extern int stdin_interaction;
  623. extern AVIOContext *progress_avio;
  624. extern float max_error_rate;
  625. extern char *filter_nbthreads;
  626. extern int filter_complex_nbthreads;
  627. extern int vstats_version;
  628. extern int auto_conversion_filters;
  629. extern const AVIOInterruptCB int_cb;
  630. extern const OptionDef options[];
  631. extern HWDevice *filter_hw_device;
  632. extern unsigned nb_output_dumped;
  633. extern int main_return_code;
  634. extern int ignore_unknown_streams;
  635. extern int copy_unknown_streams;
  636. extern int recast_media;
  637. #if FFMPEG_OPT_PSNR
  638. extern int do_psnr;
  639. #endif
  640. void term_init(void);
  641. void term_exit(void);
  642. void show_usage(void);
  643. void remove_avoptions(AVDictionary **a, AVDictionary *b);
  644. void assert_avoptions(AVDictionary *m);
  645. void assert_file_overwrite(const char *filename);
  646. char *file_read(const char *filename);
  647. AVDictionary *strip_specifiers(const AVDictionary *dict);
  648. const AVCodec *find_codec_or_die(void *logctx, const char *name,
  649. enum AVMediaType type, int encoder);
  650. int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_idx, int is_global);
  651. int configure_filtergraph(FilterGraph *fg);
  652. void check_filter_outputs(void);
  653. int filtergraph_is_simple(FilterGraph *fg);
  654. int init_simple_filtergraph(InputStream *ist, OutputStream *ost);
  655. int init_complex_filtergraph(FilterGraph *fg);
  656. void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub);
  657. int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame);
  658. int ffmpeg_parse_options(int argc, char **argv);
  659. void enc_stats_write(OutputStream *ost, EncStats *es,
  660. const AVFrame *frame, const AVPacket *pkt,
  661. uint64_t frame_num);
  662. HWDevice *hw_device_get_by_name(const char *name);
  663. int hw_device_init_from_string(const char *arg, HWDevice **dev);
  664. void hw_device_free_all(void);
  665. int hw_device_setup_for_decode(InputStream *ist);
  666. int hw_device_setup_for_encode(OutputStream *ost);
  667. int hw_device_setup_for_filter(FilterGraph *fg);
  668. int hwaccel_decode_init(AVCodecContext *avctx);
  669. /*
  670. * Initialize muxing state for the given stream, should be called
  671. * after the codec/streamcopy setup has been done.
  672. *
  673. * Open the muxer once all the streams have been initialized.
  674. */
  675. int of_stream_init(OutputFile *of, OutputStream *ost);
  676. int of_write_trailer(OutputFile *of);
  677. int of_open(const OptionsContext *o, const char *filename);
  678. void of_close(OutputFile **pof);
  679. void of_enc_stats_close(void);
  680. /*
  681. * Send a single packet to the output, applying any bitstream filters
  682. * associated with the output stream. This may result in any number
  683. * of packets actually being written, depending on what bitstream
  684. * filters are applied. The supplied packet is consumed and will be
  685. * blank (as if newly-allocated) when this function returns.
  686. *
  687. * If eof is set, instead indicate EOF to all bitstream filters and
  688. * therefore flush any delayed packets to the output. A blank packet
  689. * must be supplied in this case.
  690. */
  691. void of_output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof);
  692. int64_t of_filesize(OutputFile *of);
  693. int ifile_open(const OptionsContext *o, const char *filename);
  694. void ifile_close(InputFile **f);
  695. /**
  696. * Get next input packet from the demuxer.
  697. *
  698. * @param pkt the packet is written here when this function returns 0
  699. * @return
  700. * - 0 when a packet has been read successfully
  701. * - 1 when stream end was reached, but the stream is looped;
  702. * caller should flush decoders and read from this demuxer again
  703. * - a negative error code on failure
  704. */
  705. int ifile_get_packet(InputFile *f, AVPacket **pkt);
  706. /* iterate over all input streams in all input files;
  707. * pass NULL to start iteration */
  708. InputStream *ist_iter(InputStream *prev);
  709. #define SPECIFIER_OPT_FMT_str "%s"
  710. #define SPECIFIER_OPT_FMT_i "%i"
  711. #define SPECIFIER_OPT_FMT_i64 "%"PRId64
  712. #define SPECIFIER_OPT_FMT_ui64 "%"PRIu64
  713. #define SPECIFIER_OPT_FMT_f "%f"
  714. #define SPECIFIER_OPT_FMT_dbl "%lf"
  715. #define WARN_MULTIPLE_OPT_USAGE(name, type, so, st)\
  716. {\
  717. char namestr[128] = "";\
  718. const char *spec = so->specifier && so->specifier[0] ? so->specifier : "";\
  719. for (int _i = 0; opt_name_##name[_i]; _i++)\
  720. av_strlcatf(namestr, sizeof(namestr), "-%s%s", opt_name_##name[_i], opt_name_##name[_i+1] ? (opt_name_##name[_i+2] ? ", " : " or ") : "");\
  721. av_log(NULL, AV_LOG_WARNING, "Multiple %s options specified for stream %d, only the last option '-%s%s%s "SPECIFIER_OPT_FMT_##type"' will be used.\n",\
  722. namestr, st->index, opt_name_##name[0], spec[0] ? ":" : "", spec, so->u.type);\
  723. }
  724. #define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
  725. {\
  726. int _ret, _matches = 0;\
  727. SpecifierOpt *so;\
  728. for (int _i = 0; _i < o->nb_ ## name; _i++) {\
  729. char *spec = o->name[_i].specifier;\
  730. if ((_ret = check_stream_specifier(fmtctx, st, spec)) > 0) {\
  731. outvar = o->name[_i].u.type;\
  732. so = &o->name[_i];\
  733. _matches++;\
  734. } else if (_ret < 0)\
  735. exit_program(1);\
  736. }\
  737. if (_matches > 1)\
  738. WARN_MULTIPLE_OPT_USAGE(name, type, so, st);\
  739. }
  740. #define MATCH_PER_TYPE_OPT(name, type, outvar, fmtctx, mediatype)\
  741. {\
  742. int i;\
  743. for (i = 0; i < o->nb_ ## name; i++) {\
  744. char *spec = o->name[i].specifier;\
  745. if (!strcmp(spec, mediatype))\
  746. outvar = o->name[i].u.type;\
  747. }\
  748. }
  749. extern const char * const opt_name_codec_names[];
  750. extern const char * const opt_name_codec_tags[];
  751. extern const char * const opt_name_frame_rates[];
  752. extern const char * const opt_name_top_field_first[];
  753. #endif /* FFTOOLS_FFMPEG_H */