]> git.sesse.net Git - ffmpeg/blob - ffmpeg.h
mpegvideo: fix MB/MV vissualization on videos that are not mod 16 == 0
[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 #include "libavfilter/avfiltergraph.h"
41
42 #include "libavutil/avutil.h"
43 #include "libavutil/dict.h"
44 #include "libavutil/fifo.h"
45 #include "libavutil/pixfmt.h"
46 #include "libavutil/rational.h"
47
48 #include "libswresample/swresample.h"
49
50 #define VSYNC_AUTO       -1
51 #define VSYNC_PASSTHROUGH 0
52 #define VSYNC_CFR         1
53 #define VSYNC_VFR         2
54 #define VSYNC_DROP        0xff
55
56 #define MAX_STREAMS 1024    /* arbitrary sanity check value */
57
58 /* select an input stream for an output stream */
59 typedef struct StreamMap {
60     int disabled;           /** 1 is this mapping is disabled by a negative map */
61     int file_index;
62     int stream_index;
63     int sync_file_index;
64     int sync_stream_index;
65     char *linklabel;       /** name of an output link, for mapping lavfi outputs */
66 } StreamMap;
67
68 typedef struct {
69     int  file_idx,  stream_idx,  channel_idx; // input
70     int ofile_idx, ostream_idx;               // output
71 } AudioChannelMap;
72
73 typedef struct OptionsContext {
74     /* input/output options */
75     int64_t start_time;
76     const char *format;
77
78     SpecifierOpt *codec_names;
79     int        nb_codec_names;
80     SpecifierOpt *audio_channels;
81     int        nb_audio_channels;
82     SpecifierOpt *audio_sample_rate;
83     int        nb_audio_sample_rate;
84     SpecifierOpt *frame_rates;
85     int        nb_frame_rates;
86     SpecifierOpt *frame_sizes;
87     int        nb_frame_sizes;
88     SpecifierOpt *frame_pix_fmts;
89     int        nb_frame_pix_fmts;
90
91     /* input options */
92     int64_t input_ts_offset;
93     int rate_emu;
94
95     SpecifierOpt *ts_scale;
96     int        nb_ts_scale;
97     SpecifierOpt *dump_attachment;
98     int        nb_dump_attachment;
99
100     /* output options */
101     StreamMap *stream_maps;
102     int     nb_stream_maps;
103     AudioChannelMap *audio_channel_maps; /* one info entry per -map_channel */
104     int           nb_audio_channel_maps; /* number of (valid) -map_channel settings */
105     int metadata_global_manual;
106     int metadata_streams_manual;
107     int metadata_chapters_manual;
108     const char **attachments;
109     int       nb_attachments;
110
111     int chapters_input_file;
112
113     int64_t recording_time;
114     uint64_t limit_filesize;
115     float mux_preload;
116     float mux_max_delay;
117
118     int video_disable;
119     int audio_disable;
120     int subtitle_disable;
121     int data_disable;
122
123     /* indexed by output file stream index */
124     int   *streamid_map;
125     int nb_streamid_map;
126
127     SpecifierOpt *metadata;
128     int        nb_metadata;
129     SpecifierOpt *max_frames;
130     int        nb_max_frames;
131     SpecifierOpt *bitstream_filters;
132     int        nb_bitstream_filters;
133     SpecifierOpt *codec_tags;
134     int        nb_codec_tags;
135     SpecifierOpt *sample_fmts;
136     int        nb_sample_fmts;
137     SpecifierOpt *qscale;
138     int        nb_qscale;
139     SpecifierOpt *forced_key_frames;
140     int        nb_forced_key_frames;
141     SpecifierOpt *force_fps;
142     int        nb_force_fps;
143     SpecifierOpt *frame_aspect_ratios;
144     int        nb_frame_aspect_ratios;
145     SpecifierOpt *rc_overrides;
146     int        nb_rc_overrides;
147     SpecifierOpt *intra_matrices;
148     int        nb_intra_matrices;
149     SpecifierOpt *inter_matrices;
150     int        nb_inter_matrices;
151     SpecifierOpt *top_field_first;
152     int        nb_top_field_first;
153     SpecifierOpt *metadata_map;
154     int        nb_metadata_map;
155     SpecifierOpt *presets;
156     int        nb_presets;
157     SpecifierOpt *copy_initial_nonkeyframes;
158     int        nb_copy_initial_nonkeyframes;
159     SpecifierOpt *filters;
160     int        nb_filters;
161     SpecifierOpt *fix_sub_duration;
162     int        nb_fix_sub_duration;
163 } OptionsContext;
164
165 typedef struct InputFilter {
166     AVFilterContext    *filter;
167     struct InputStream *ist;
168     struct FilterGraph *graph;
169     uint8_t            *name;
170 } InputFilter;
171
172 typedef struct OutputFilter {
173     AVFilterContext     *filter;
174     struct OutputStream *ost;
175     struct FilterGraph  *graph;
176     uint8_t             *name;
177
178     /* temporary storage until stream maps are processed */
179     AVFilterInOut       *out_tmp;
180 } OutputFilter;
181
182 typedef struct FilterGraph {
183     int            index;
184     const char    *graph_desc;
185
186     AVFilterGraph *graph;
187
188     InputFilter   **inputs;
189     int          nb_inputs;
190     OutputFilter **outputs;
191     int         nb_outputs;
192 } FilterGraph;
193
194 typedef struct InputStream {
195     int file_index;
196     AVStream *st;
197     int discard;             /* true if stream data should be discarded */
198     int decoding_needed;     /* true if the packets must be decoded in 'raw_fifo' */
199     AVCodec *dec;
200     AVFrame *decoded_frame;
201
202     int64_t       start;     /* time when read started */
203     /* predicted dts of the next packet read for this stream or (when there are
204      * several frames in a packet) of the next frame in current packet (in AV_TIME_BASE units) */
205     int64_t       next_dts;
206     int64_t       dts;       ///< dts of the last packet read for this stream (in AV_TIME_BASE units)
207
208     int64_t       next_pts;  ///< synthetic pts for the next decode frame (in AV_TIME_BASE units)
209     int64_t       pts;       ///< current pts of the decoded frame  (in AV_TIME_BASE units)
210     int           wrap_correction_done;
211     double ts_scale;
212     int is_start;            /* is 1 at the start and after a discontinuity */
213     int saw_first_ts;
214     int showed_multi_packet_warning;
215     AVDictionary *opts;
216     AVRational framerate;               /* framerate forced with -r */
217     int top_field_first;
218
219     int resample_height;
220     int resample_width;
221     int resample_pix_fmt;
222
223     int      resample_sample_fmt;
224     int      resample_sample_rate;
225     int      resample_channels;
226     uint64_t resample_channel_layout;
227
228     int fix_sub_duration;
229     struct { /* previous decoded subtitle and related variables */
230         int64_t pts;
231         int got_output;
232         int ret;
233         AVSubtitle subtitle;
234     } prev_sub;
235
236     struct sub2video {
237         int64_t last_pts;
238         AVFilterBufferRef *ref;
239         int w, h;
240     } sub2video;
241
242     /* a pool of free buffers for decoded data */
243     FrameBuffer *buffer_pool;
244     int dr1;
245
246     /* decoded data from this stream goes into all those filters
247      * currently video and audio only */
248     InputFilter **filters;
249     int        nb_filters;
250 } InputStream;
251
252 typedef struct InputFile {
253     AVFormatContext *ctx;
254     int eof_reached;      /* true if eof reached */
255     int eagain;           /* true if last read attempt returned EAGAIN */
256     int ist_index;        /* index of first stream in input_streams */
257     int64_t ts_offset;
258     int nb_streams;       /* number of stream that ffmpeg is aware of; may be different
259                              from ctx.nb_streams if new streams appear during av_read_frame() */
260     int nb_streams_warn;  /* number of streams that the user was warned of */
261     int rate_emu;
262
263 #if HAVE_PTHREADS
264     pthread_t thread;           /* thread reading from this file */
265     int finished;               /* the thread has exited */
266     int joined;                 /* the thread has been joined */
267     pthread_mutex_t fifo_lock;  /* lock for access to fifo */
268     pthread_cond_t  fifo_cond;  /* the main thread will signal on this cond after reading from fifo */
269     AVFifoBuffer *fifo;         /* demuxed packets are stored here; freed by the main thread */
270 #endif
271 } InputFile;
272
273 typedef struct OutputStream {
274     int file_index;          /* file index */
275     int index;               /* stream index in the output file */
276     int source_index;        /* InputStream index */
277     AVStream *st;            /* stream in the output file */
278     int encoding_needed;     /* true if encoding needed for this stream */
279     int frame_number;
280     /* input pts and corresponding output pts
281        for A/V sync */
282     struct InputStream *sync_ist; /* input stream to sync against */
283     int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
284     /* pts of the first frame encoded for this stream, used for limiting
285      * recording time */
286     int64_t first_pts;
287     AVBitStreamFilterContext *bitstream_filters;
288     AVCodec *enc;
289     int64_t max_frames;
290     AVFrame *filtered_frame;
291
292     /* video only */
293     AVRational frame_rate;
294     int force_fps;
295     int top_field_first;
296
297     float frame_aspect_ratio;
298     float last_quality;
299
300     /* forced key frames */
301     int64_t *forced_kf_pts;
302     int forced_kf_count;
303     int forced_kf_index;
304     char *forced_keyframes;
305
306     /* audio only */
307     int audio_channels_map[SWR_CH_MAX];  /* list of the channels id to pick from the source stream */
308     int audio_channels_mapped;           /* number of channels in audio_channels_map */
309
310     FILE *logfile;
311
312     OutputFilter *filter;
313     char *avfilter;
314
315     int64_t sws_flags;
316     int64_t swr_dither_method;
317     double swr_dither_scale;
318     AVDictionary *opts;
319     int finished;        /* no more packets should be written for this stream */
320     int unavailable;                     /* true if the steram is unavailable (possibly temporarily) */
321     int stream_copy;
322     const char *attachment_filename;
323     int copy_initial_nonkeyframes;
324
325     int keep_pix_fmt;
326 } OutputStream;
327
328 typedef struct OutputFile {
329     AVFormatContext *ctx;
330     AVDictionary *opts;
331     int ost_index;       /* index of the first stream in output_streams */
332     int64_t recording_time;  ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
333     int64_t start_time;      ///< start time in microseconds == AV_TIME_BASE units
334     uint64_t limit_filesize; /* filesize limit expressed in bytes */
335 } OutputFile;
336
337 extern InputStream **input_streams;
338 extern int        nb_input_streams;
339 extern InputFile   **input_files;
340 extern int        nb_input_files;
341
342 extern OutputStream **output_streams;
343 extern int         nb_output_streams;
344 extern OutputFile   **output_files;
345 extern int         nb_output_files;
346
347 extern FilterGraph **filtergraphs;
348 extern int        nb_filtergraphs;
349
350 extern const char *pass_logfilename_prefix;
351 extern char *vstats_filename;
352
353 extern float audio_drift_threshold;
354 extern float dts_delta_threshold;
355 extern float dts_error_threshold;
356
357 extern int audio_volume;
358 extern int audio_sync_method;
359 extern int video_sync_method;
360 extern int do_benchmark;
361 extern int do_benchmark_all;
362 extern int do_deinterlace;
363 extern int do_hex_dump;
364 extern int do_pkt_dump;
365 extern int copy_ts;
366 extern int copy_tb;
367 extern int debug_ts;
368 extern int opt_shortest;
369 extern int exit_on_error;
370 extern int print_stats;
371 extern int qp_hist;
372 extern int same_quant;
373 extern int stdin_interaction;
374 extern int frame_bits_per_raw_sample;
375 extern AVIOContext *progress_avio;
376
377 extern const AVIOInterruptCB int_cb;
378
379 extern const OptionDef options[];
380
381 void term_init(void);
382 void term_exit(void);
383
384 void reset_options(OptionsContext *o, int is_input);
385 void show_usage(void);
386
387 void opt_output_file(void *optctx, const char *filename);
388
389 void assert_avoptions(AVDictionary *m);
390
391 int guess_input_channel_layout(InputStream *ist);
392
393 enum PixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum PixelFormat target);
394 void choose_sample_fmt(AVStream *st, AVCodec *codec);
395
396 int configure_filtergraph(FilterGraph *fg);
397 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
398 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
399 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
400
401 #endif /* FFMPEG_H */