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