978195206ee30186ddc5e2afeeb613c177dc83b4
[libav.git] / avconv.h
1 /*
2 * This file is part of Libav.
3 *
4 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #ifndef AVCONV_H
20 #define AVCONV_H
21
22 #include "config.h"
23
24 #include <stdint.h>
25 #include <stdio.h>
26
27 #if HAVE_PTHREADS
28 #include <pthread.h>
29 #endif
30
31 #include "cmdutils.h"
32
33 #include "libavformat/avformat.h"
34 #include "libavformat/avio.h"
35
36 #include "libavcodec/avcodec.h"
37
38 #include "libavfilter/avfilter.h"
39
40 #include "libavutil/avutil.h"
41 #include "libavutil/dict.h"
42 #include "libavutil/fifo.h"
43 #include "libavutil/pixfmt.h"
44 #include "libavutil/rational.h"
45
46 #define VSYNC_AUTO -1
47 #define VSYNC_PASSTHROUGH 0
48 #define VSYNC_CFR 1
49 #define VSYNC_VFR 2
50
51 enum HWAccelID {
52 HWACCEL_NONE = 0,
53 HWACCEL_AUTO,
54 };
55
56 typedef struct HWAccel {
57 const char *name;
58 int (*init)(AVCodecContext *s);
59 enum HWAccelID id;
60 enum AVPixelFormat pix_fmt;
61 } HWAccel;
62
63 /* select an input stream for an output stream */
64 typedef struct StreamMap {
65 int disabled; /* 1 is this mapping is disabled by a negative map */
66 int file_index;
67 int stream_index;
68 int sync_file_index;
69 int sync_stream_index;
70 char *linklabel; /* name of an output link, for mapping lavfi outputs */
71 } StreamMap;
72
73 /* select an input file for an output file */
74 typedef struct MetadataMap {
75 int file; // file index
76 char type; // type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram
77 int index; // stream/chapter/program number
78 } MetadataMap;
79
80 typedef struct OptionsContext {
81 OptionGroup *g;
82
83 /* input/output options */
84 int64_t start_time;
85 const char *format;
86
87 SpecifierOpt *codec_names;
88 int nb_codec_names;
89 SpecifierOpt *audio_channels;
90 int nb_audio_channels;
91 SpecifierOpt *audio_sample_rate;
92 int nb_audio_sample_rate;
93 SpecifierOpt *frame_rates;
94 int nb_frame_rates;
95 SpecifierOpt *frame_sizes;
96 int nb_frame_sizes;
97 SpecifierOpt *frame_pix_fmts;
98 int nb_frame_pix_fmts;
99
100 /* input options */
101 int64_t input_ts_offset;
102 int rate_emu;
103 int accurate_seek;
104
105 SpecifierOpt *ts_scale;
106 int nb_ts_scale;
107 SpecifierOpt *dump_attachment;
108 int nb_dump_attachment;
109 SpecifierOpt *hwaccels;
110 int nb_hwaccels;
111 SpecifierOpt *hwaccel_devices;
112 int nb_hwaccel_devices;
113
114 /* output options */
115 StreamMap *stream_maps;
116 int nb_stream_maps;
117 /* first item specifies output metadata, second is input */
118 MetadataMap (*meta_data_maps)[2];
119 int nb_meta_data_maps;
120 int metadata_global_manual;
121 int metadata_streams_manual;
122 int metadata_chapters_manual;
123 const char **attachments;
124 int nb_attachments;
125
126 int chapters_input_file;
127
128 int64_t recording_time;
129 uint64_t limit_filesize;
130 float mux_preload;
131 float mux_max_delay;
132 int shortest;
133
134 int video_disable;
135 int audio_disable;
136 int subtitle_disable;
137 int data_disable;
138
139 /* indexed by output file stream index */
140 int *streamid_map;
141 int nb_streamid_map;
142
143 SpecifierOpt *metadata;
144 int nb_metadata;
145 SpecifierOpt *max_frames;
146 int nb_max_frames;
147 SpecifierOpt *bitstream_filters;
148 int nb_bitstream_filters;
149 SpecifierOpt *codec_tags;
150 int nb_codec_tags;
151 SpecifierOpt *sample_fmts;
152 int nb_sample_fmts;
153 SpecifierOpt *qscale;
154 int nb_qscale;
155 SpecifierOpt *forced_key_frames;
156 int nb_forced_key_frames;
157 SpecifierOpt *force_fps;
158 int nb_force_fps;
159 SpecifierOpt *frame_aspect_ratios;
160 int nb_frame_aspect_ratios;
161 SpecifierOpt *rc_overrides;
162 int nb_rc_overrides;
163 SpecifierOpt *intra_matrices;
164 int nb_intra_matrices;
165 SpecifierOpt *inter_matrices;
166 int nb_inter_matrices;
167 SpecifierOpt *top_field_first;
168 int nb_top_field_first;
169 SpecifierOpt *metadata_map;
170 int nb_metadata_map;
171 SpecifierOpt *presets;
172 int nb_presets;
173 SpecifierOpt *copy_initial_nonkeyframes;
174 int nb_copy_initial_nonkeyframes;
175 SpecifierOpt *filters;
176 int nb_filters;
177 SpecifierOpt *filter_scripts;
178 int nb_filter_scripts;
179 SpecifierOpt *pass;
180 int nb_pass;
181 SpecifierOpt *passlogfiles;
182 int nb_passlogfiles;
183 } OptionsContext;
184
185 typedef struct InputFilter {
186 AVFilterContext *filter;
187 struct InputStream *ist;
188 struct FilterGraph *graph;
189 uint8_t *name;
190 } InputFilter;
191
192 typedef struct OutputFilter {
193 AVFilterContext *filter;
194 struct OutputStream *ost;
195 struct FilterGraph *graph;
196 uint8_t *name;
197
198 /* temporary storage until stream maps are processed */
199 AVFilterInOut *out_tmp;
200 } OutputFilter;
201
202 typedef struct FilterGraph {
203 int index;
204 const char *graph_desc;
205
206 AVFilterGraph *graph;
207
208 InputFilter **inputs;
209 int nb_inputs;
210 OutputFilter **outputs;
211 int nb_outputs;
212 } FilterGraph;
213
214 typedef struct InputStream {
215 int file_index;
216 AVStream *st;
217 int discard; /* true if stream data should be discarded */
218 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
219 AVCodec *dec;
220 AVFrame *decoded_frame;
221 AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
222
223 int64_t start; /* time when read started */
224 /* predicted dts of the next packet read for this stream or (when there are
225 * several frames in a packet) of the next frame in current packet */
226 int64_t next_dts;
227 /* dts of the last packet read for this stream */
228 int64_t last_dts;
229 PtsCorrectionContext pts_ctx;
230 double ts_scale;
231 int is_start; /* is 1 at the start and after a discontinuity */
232 int showed_multi_packet_warning;
233 AVDictionary *opts;
234 AVRational framerate; /* framerate forced with -r */
235
236 int resample_height;
237 int resample_width;
238 int resample_pix_fmt;
239
240 int resample_sample_fmt;
241 int resample_sample_rate;
242 int resample_channels;
243 uint64_t resample_channel_layout;
244
245 /* decoded data from this stream goes into all those filters
246 * currently video and audio only */
247 InputFilter **filters;
248 int nb_filters;
249
250 /* hwaccel options */
251 enum HWAccelID hwaccel_id;
252 char *hwaccel_device;
253
254 /* hwaccel context */
255 enum HWAccelID active_hwaccel_id;
256 void *hwaccel_ctx;
257 void (*hwaccel_uninit)(AVCodecContext *s);
258 int (*hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags);
259 int (*hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame);
260 enum AVPixelFormat hwaccel_pix_fmt;
261 enum AVPixelFormat hwaccel_retrieved_pix_fmt;
262 } InputStream;
263
264 typedef struct InputFile {
265 AVFormatContext *ctx;
266 int eof_reached; /* true if eof reached */
267 int eagain; /* true if last read attempt returned EAGAIN */
268 int ist_index; /* index of first stream in ist_table */
269 int64_t ts_offset;
270 int64_t start_time; /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
271 int64_t recording_time;
272 int nb_streams; /* number of stream that avconv is aware of; may be different
273 from ctx.nb_streams if new streams appear during av_read_frame() */
274 int rate_emu;
275 int accurate_seek;
276
277 #if HAVE_PTHREADS
278 pthread_t thread; /* thread reading from this file */
279 int finished; /* the thread has exited */
280 int joined; /* the thread has been joined */
281 pthread_mutex_t fifo_lock; /* lock for access to fifo */
282 pthread_cond_t fifo_cond; /* the main thread will signal on this cond after reading from fifo */
283 AVFifoBuffer *fifo; /* demuxed packets are stored here; freed by the main thread */
284 #endif
285 } InputFile;
286
287 typedef struct OutputStream {
288 int file_index; /* file index */
289 int index; /* stream index in the output file */
290 int source_index; /* InputStream index */
291 AVStream *st; /* stream in the output file */
292 int encoding_needed; /* true if encoding needed for this stream */
293 int frame_number;
294 /* input pts and corresponding output pts
295 for A/V sync */
296 // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
297 struct InputStream *sync_ist; /* input stream to sync against */
298 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
299 /* pts of the first frame encoded for this stream, used for limiting
300 * recording time */
301 int64_t first_pts;
302 /* dts of the last packet sent to the muxer */
303 int64_t last_mux_dts;
304 AVBitStreamFilterContext *bitstream_filters;
305 AVCodec *enc;
306 int64_t max_frames;
307 AVFrame *filtered_frame;
308
309 /* video only */
310 AVRational frame_rate;
311 int force_fps;
312 int top_field_first;
313
314 float frame_aspect_ratio;
315
316 /* forced key frames */
317 int64_t *forced_kf_pts;
318 int forced_kf_count;
319 int forced_kf_index;
320 char *forced_keyframes;
321
322 char *logfile_prefix;
323 FILE *logfile;
324
325 OutputFilter *filter;
326 char *avfilter;
327
328 int64_t sws_flags;
329 AVDictionary *opts;
330 AVDictionary *resample_opts;
331 int finished; /* no more packets should be written for this stream */
332 int stream_copy;
333 const char *attachment_filename;
334 int copy_initial_nonkeyframes;
335
336 enum AVPixelFormat pix_fmts[2];
337
338 AVCodecParserContext *parser;
339 } OutputStream;
340
341 typedef struct OutputFile {
342 AVFormatContext *ctx;
343 AVDictionary *opts;
344 int ost_index; /* index of the first stream in output_streams */
345 int64_t recording_time; /* desired length of the resulting file in microseconds */
346 int64_t start_time; /* start time in microseconds */
347 uint64_t limit_filesize;
348
349 int shortest;
350 } OutputFile;
351
352 extern InputStream **input_streams;
353 extern int nb_input_streams;
354 extern InputFile **input_files;
355 extern int nb_input_files;
356
357 extern OutputStream **output_streams;
358 extern int nb_output_streams;
359 extern OutputFile **output_files;
360 extern int nb_output_files;
361
362 extern FilterGraph **filtergraphs;
363 extern int nb_filtergraphs;
364
365 extern char *vstats_filename;
366
367 extern float audio_drift_threshold;
368 extern float dts_delta_threshold;
369
370 extern int audio_volume;
371 extern int audio_sync_method;
372 extern int video_sync_method;
373 extern int do_benchmark;
374 extern int do_deinterlace;
375 extern int do_hex_dump;
376 extern int do_pkt_dump;
377 extern int copy_ts;
378 extern int copy_tb;
379 extern int exit_on_error;
380 extern int print_stats;
381 extern int qp_hist;
382
383 extern const AVIOInterruptCB int_cb;
384
385 extern const OptionDef options[];
386
387 extern const HWAccel hwaccels[];
388
389 void reset_options(OptionsContext *o);
390 void show_usage(void);
391
392 void opt_output_file(void *optctx, const char *filename);
393
394 void assert_avoptions(AVDictionary *m);
395
396 int guess_input_channel_layout(InputStream *ist);
397
398 int configure_filtergraph(FilterGraph *fg);
399 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out);
400 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist);
401 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost);
402
403 int avconv_parse_options(int argc, char **argv);
404
405 #endif /* AVCONV_H */