Commit | Line | Data |
---|---|---|
6291d7e4 AK |
1 | /* |
2 | * avconv main | |
3 | * Copyright (c) 2000-2011 The libav developers. | |
4 | * | |
5 | * This file is part of Libav. | |
6 | * | |
7 | * Libav is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2.1 of the License, or (at your option) any later version. | |
11 | * | |
12 | * Libav is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with Libav; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include "config.h" | |
23 | #include <ctype.h> | |
24 | #include <string.h> | |
25 | #include <math.h> | |
26 | #include <stdlib.h> | |
27 | #include <errno.h> | |
28 | #include <signal.h> | |
29 | #include <limits.h> | |
30 | #include <unistd.h> | |
31 | #include "libavformat/avformat.h" | |
32 | #include "libavdevice/avdevice.h" | |
33 | #include "libswscale/swscale.h" | |
34 | #include "libavutil/opt.h" | |
35 | #include "libavcodec/audioconvert.h" | |
36 | #include "libavutil/audioconvert.h" | |
37 | #include "libavutil/parseutils.h" | |
38 | #include "libavutil/samplefmt.h" | |
39 | #include "libavutil/colorspace.h" | |
40 | #include "libavutil/fifo.h" | |
41 | #include "libavutil/intreadwrite.h" | |
42 | #include "libavutil/dict.h" | |
43 | #include "libavutil/mathematics.h" | |
44 | #include "libavutil/pixdesc.h" | |
45 | #include "libavutil/avstring.h" | |
46 | #include "libavutil/libm.h" | |
47 | #include "libavformat/os_support.h" | |
48 | ||
49 | #if CONFIG_AVFILTER | |
50 | # include "libavfilter/avfilter.h" | |
51 | # include "libavfilter/avfiltergraph.h" | |
52 | # include "libavfilter/vsrc_buffer.h" | |
53 | #endif | |
54 | ||
55 | #if HAVE_SYS_RESOURCE_H | |
56 | #include <sys/types.h> | |
57 | #include <sys/time.h> | |
58 | #include <sys/resource.h> | |
59 | #elif HAVE_GETPROCESSTIMES | |
60 | #include <windows.h> | |
61 | #endif | |
62 | #if HAVE_GETPROCESSMEMORYINFO | |
63 | #include <windows.h> | |
64 | #include <psapi.h> | |
65 | #endif | |
66 | ||
67 | #if HAVE_SYS_SELECT_H | |
68 | #include <sys/select.h> | |
69 | #endif | |
70 | ||
71 | #include <time.h> | |
72 | ||
73 | #include "cmdutils.h" | |
74 | ||
75 | #include "libavutil/avassert.h" | |
76 | ||
77 | const char program_name[] = "avconv"; | |
78 | const int program_birth_year = 2000; | |
79 | ||
80 | /* select an input stream for an output stream */ | |
81 | typedef struct StreamMap { | |
8d2e4a7e | 82 | int disabled; /** 1 is this mapping is disabled by a negative map */ |
6291d7e4 AK |
83 | int file_index; |
84 | int stream_index; | |
85 | int sync_file_index; | |
86 | int sync_stream_index; | |
87 | } StreamMap; | |
88 | ||
89 | /** | |
90 | * select an input file for an output file | |
91 | */ | |
92 | typedef struct MetadataMap { | |
02494787 DB |
93 | int file; ///< file index |
94 | char type; ///< type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram | |
95 | int index; ///< stream/chapter/program number | |
6291d7e4 AK |
96 | } MetadataMap; |
97 | ||
6291d7e4 AK |
98 | static const OptionDef options[]; |
99 | ||
6291d7e4 AK |
100 | /* indexed by output file stream index */ |
101 | static int *streamid_map = NULL; | |
102 | static int nb_streamid_map = 0; | |
103 | ||
104 | static int frame_width = 0; | |
105 | static int frame_height = 0; | |
106 | static float frame_aspect_ratio = 0; | |
107 | static enum PixelFormat frame_pix_fmt = PIX_FMT_NONE; | |
6291d7e4 | 108 | static AVRational frame_rate; |
6291d7e4 AK |
109 | static uint16_t *intra_matrix = NULL; |
110 | static uint16_t *inter_matrix = NULL; | |
111 | static const char *video_rc_override_string=NULL; | |
6291d7e4 | 112 | static int video_discard = 0; |
f4ad238c | 113 | static int same_quant = 0; |
6291d7e4 AK |
114 | static int do_deinterlace = 0; |
115 | static int top_field_first = -1; | |
116 | static int me_threshold = 0; | |
117 | static int intra_dc_precision = 8; | |
6291d7e4 AK |
118 | static int qp_hist = 0; |
119 | #if CONFIG_AVFILTER | |
120 | static char *vfilters = NULL; | |
121 | #endif | |
122 | ||
6291d7e4 | 123 | static int file_overwrite = 0; |
6291d7e4 AK |
124 | static int do_benchmark = 0; |
125 | static int do_hex_dump = 0; | |
126 | static int do_pkt_dump = 0; | |
127 | static int do_psnr = 0; | |
128 | static int do_pass = 0; | |
129 | static char *pass_logfilename_prefix = NULL; | |
6291d7e4 AK |
130 | static int video_sync_method= -1; |
131 | static int audio_sync_method= 0; | |
132 | static float audio_drift_threshold= 0.1; | |
133 | static int copy_ts= 0; | |
134 | static int copy_tb; | |
135 | static int opt_shortest = 0; | |
136 | static char *vstats_filename; | |
137 | static FILE *vstats_file; | |
6291d7e4 AK |
138 | static int copy_initial_nonkeyframes = 0; |
139 | ||
6291d7e4 AK |
140 | static int audio_volume = 256; |
141 | ||
142 | static int exit_on_error = 0; | |
143 | static int using_stdin = 0; | |
144 | static int verbose = 1; | |
6291d7e4 AK |
145 | static int64_t video_size = 0; |
146 | static int64_t audio_size = 0; | |
147 | static int64_t extra_size = 0; | |
148 | static int nb_frames_dup = 0; | |
149 | static int nb_frames_drop = 0; | |
150 | static int input_sync; | |
6291d7e4 AK |
151 | |
152 | static float dts_delta_threshold = 10; | |
153 | ||
6291d7e4 AK |
154 | static uint8_t *audio_buf; |
155 | static uint8_t *audio_out; | |
156 | static unsigned int allocated_audio_out_size, allocated_audio_buf_size; | |
157 | ||
158 | static short *samples; | |
159 | ||
6291d7e4 AK |
160 | #define DEFAULT_PASS_LOGFILENAME_PREFIX "av2pass" |
161 | ||
6494c001 AK |
162 | typedef struct InputStream { |
163 | int file_index; | |
164 | AVStream *st; | |
165 | int discard; /* true if stream data should be discarded */ | |
166 | int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */ | |
167 | AVCodec *dec; | |
168 | ||
169 | int64_t start; /* time when read started */ | |
170 | int64_t next_pts; /* synthetic pts for cases where pkt.pts | |
171 | is not defined */ | |
172 | int64_t pts; /* current pts */ | |
173 | PtsCorrectionContext pts_ctx; | |
174 | double ts_scale; | |
175 | int is_start; /* is 1 at the start and after a discontinuity */ | |
176 | int showed_multi_packet_warning; | |
177 | AVDictionary *opts; | |
178 | } InputStream; | |
179 | ||
180 | typedef struct InputFile { | |
181 | AVFormatContext *ctx; | |
182 | int eof_reached; /* true if eof reached */ | |
183 | int ist_index; /* index of first stream in ist_table */ | |
184 | int buffer_size; /* current total buffer size */ | |
185 | int64_t ts_offset; | |
ed5b1326 AK |
186 | int nb_streams; /* number of stream that avconv is aware of; may be different |
187 | from ctx.nb_streams if new streams appear during av_read_frame() */ | |
f4805328 | 188 | int rate_emu; |
6494c001 | 189 | } InputFile; |
6291d7e4 AK |
190 | |
191 | typedef struct OutputStream { | |
192 | int file_index; /* file index */ | |
193 | int index; /* stream index in the output file */ | |
194 | int source_index; /* InputStream index */ | |
195 | AVStream *st; /* stream in the output file */ | |
196 | int encoding_needed; /* true if encoding needed for this stream */ | |
197 | int frame_number; | |
198 | /* input pts and corresponding output pts | |
199 | for A/V sync */ | |
200 | //double sync_ipts; /* dts from the AVPacket of the demuxer in second units */ | |
201 | struct InputStream *sync_ist; /* input stream to sync against */ | |
202 | int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ //FIXME look at frame_number | |
203 | AVBitStreamFilterContext *bitstream_filters; | |
204 | AVCodec *enc; | |
96139b5e | 205 | int64_t max_frames; |
6291d7e4 AK |
206 | |
207 | /* video only */ | |
208 | int video_resample; | |
209 | AVFrame pict_tmp; /* temporary image for resampling */ | |
210 | struct SwsContext *img_resample_ctx; /* for image resampling */ | |
211 | int resample_height; | |
212 | int resample_width; | |
213 | int resample_pix_fmt; | |
214 | AVRational frame_rate; | |
bef737a7 | 215 | int force_fps; |
6291d7e4 AK |
216 | |
217 | float frame_aspect_ratio; | |
218 | ||
219 | /* forced key frames */ | |
220 | int64_t *forced_kf_pts; | |
221 | int forced_kf_count; | |
222 | int forced_kf_index; | |
223 | ||
224 | /* audio only */ | |
225 | int audio_resample; | |
226 | ReSampleContext *resample; /* for audio resampling */ | |
227 | int resample_sample_fmt; | |
228 | int resample_channels; | |
229 | int resample_sample_rate; | |
230 | int reformat_pair; | |
231 | AVAudioConvert *reformat_ctx; | |
232 | AVFifoBuffer *fifo; /* for compression: one audio fifo per codec */ | |
233 | FILE *logfile; | |
234 | ||
235 | #if CONFIG_AVFILTER | |
236 | AVFilterContext *output_video_filter; | |
237 | AVFilterContext *input_video_filter; | |
238 | AVFilterBufferRef *picref; | |
239 | char *avfilter; | |
240 | AVFilterGraph *graph; | |
241 | #endif | |
242 | ||
243 | int sws_flags; | |
244 | AVDictionary *opts; | |
ef44a607 | 245 | int is_past_recording_time; |
6291d7e4 AK |
246 | } OutputStream; |
247 | ||
6291d7e4 | 248 | |
af70aa45 AK |
249 | typedef struct OutputFile { |
250 | AVFormatContext *ctx; | |
251 | AVDictionary *opts; | |
4288e031 | 252 | int ost_index; /* index of the first stream in output_streams */ |
ef44a607 | 253 | int64_t recording_time; /* desired length of the resulting file in microseconds */ |
ea065176 | 254 | int64_t start_time; /* start time in microseconds */ |
f21f294e | 255 | uint64_t limit_filesize; |
af70aa45 AK |
256 | } OutputFile; |
257 | ||
6291d7e4 AK |
258 | static InputStream *input_streams = NULL; |
259 | static int nb_input_streams = 0; | |
260 | static InputFile *input_files = NULL; | |
261 | static int nb_input_files = 0; | |
262 | ||
4288e031 AK |
263 | static OutputStream *output_streams = NULL; |
264 | static int nb_output_streams = 0; | |
af70aa45 AK |
265 | static OutputFile *output_files = NULL; |
266 | static int nb_output_files = 0; | |
267 | ||
575ec4e1 | 268 | typedef struct OptionsContext { |
6b779ccc AK |
269 | /* input/output options */ |
270 | int64_t start_time; | |
7041bb3b | 271 | const char *format; |
6b779ccc | 272 | |
35e6f8c1 AK |
273 | SpecifierOpt *codec_names; |
274 | int nb_codec_names; | |
6a11686d AK |
275 | SpecifierOpt *audio_channels; |
276 | int nb_audio_channels; | |
e2469ccf AK |
277 | SpecifierOpt *audio_sample_rate; |
278 | int nb_audio_sample_rate; | |
35e6f8c1 | 279 | |
6b779ccc AK |
280 | /* input options */ |
281 | int64_t input_ts_offset; | |
f4805328 | 282 | int rate_emu; |
6b779ccc | 283 | |
33f75d72 AK |
284 | SpecifierOpt *ts_scale; |
285 | int nb_ts_scale; | |
286 | ||
575ec4e1 AK |
287 | /* output options */ |
288 | StreamMap *stream_maps; | |
289 | int nb_stream_maps; | |
847529f8 AK |
290 | /* first item specifies output metadata, second is input */ |
291 | MetadataMap (*meta_data_maps)[2]; | |
292 | int nb_meta_data_maps; | |
293 | int metadata_global_manual; | |
294 | int metadata_streams_manual; | |
295 | int metadata_chapters_manual; | |
6b779ccc | 296 | |
c5bb372e AK |
297 | int chapters_input_file; |
298 | ||
6b779ccc | 299 | int64_t recording_time; |
13ccba50 | 300 | uint64_t limit_filesize; |
dc26318c AK |
301 | float mux_preload; |
302 | float mux_max_delay; | |
039267f1 | 303 | |
2130981a AK |
304 | int video_disable; |
305 | int audio_disable; | |
306 | int subtitle_disable; | |
307 | int data_disable; | |
308 | ||
039267f1 AK |
309 | SpecifierOpt *metadata; |
310 | int nb_metadata; | |
96139b5e AK |
311 | SpecifierOpt *max_frames; |
312 | int nb_max_frames; | |
d821cbe2 AK |
313 | SpecifierOpt *bitstream_filters; |
314 | int nb_bitstream_filters; | |
013887eb AK |
315 | SpecifierOpt *codec_tags; |
316 | int nb_codec_tags; | |
05bffc12 AK |
317 | SpecifierOpt *sample_fmts; |
318 | int nb_sample_fmts; | |
77d9c454 AK |
319 | SpecifierOpt *qscale; |
320 | int nb_qscale; | |
7c029672 AK |
321 | SpecifierOpt *forced_key_frames; |
322 | int nb_forced_key_frames; | |
bef737a7 AK |
323 | SpecifierOpt *force_fps; |
324 | int nb_force_fps; | |
575ec4e1 AK |
325 | } OptionsContext; |
326 | ||
35e6f8c1 AK |
327 | #define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\ |
328 | {\ | |
329 | int i, ret;\ | |
330 | for (i = 0; i < o->nb_ ## name; i++) {\ | |
331 | char *spec = o->name[i].specifier;\ | |
332 | if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0)\ | |
333 | outvar = o->name[i].u.type;\ | |
334 | else if (ret < 0)\ | |
335 | exit_program(1);\ | |
336 | }\ | |
337 | } | |
338 | ||
575ec4e1 AK |
339 | static void reset_options(OptionsContext *o) |
340 | { | |
341 | const OptionDef *po = options; | |
342 | ||
343 | /* all OPT_SPEC and OPT_STRING can be freed in generic way */ | |
344 | while (po->name) { | |
345 | void *dst = (uint8_t*)o + po->u.off; | |
346 | ||
347 | if (po->flags & OPT_SPEC) { | |
348 | SpecifierOpt **so = dst; | |
349 | int i, *count = (int*)(so + 1); | |
350 | for (i = 0; i < *count; i++) { | |
351 | av_freep(&(*so)[i].specifier); | |
352 | if (po->flags & OPT_STRING) | |
353 | av_freep(&(*so)[i].u.str); | |
354 | } | |
355 | av_freep(so); | |
356 | *count = 0; | |
357 | } else if (po->flags & OPT_OFFSET && po->flags & OPT_STRING) | |
358 | av_freep(dst); | |
359 | po++; | |
360 | } | |
361 | ||
362 | av_freep(&o->stream_maps); | |
847529f8 | 363 | av_freep(&o->meta_data_maps); |
575ec4e1 AK |
364 | |
365 | memset(o, 0, sizeof(*o)); | |
6b779ccc | 366 | |
dc26318c AK |
367 | o->mux_preload = 0.5; |
368 | o->mux_max_delay = 0.7; | |
6b779ccc | 369 | o->recording_time = INT64_MAX; |
13ccba50 | 370 | o->limit_filesize = UINT64_MAX; |
c5bb372e | 371 | o->chapters_input_file = INT_MAX; |
6b779ccc | 372 | |
575ec4e1 AK |
373 | uninit_opts(); |
374 | init_opts(); | |
375 | } | |
376 | ||
6291d7e4 AK |
377 | #if CONFIG_AVFILTER |
378 | ||
379 | static int configure_video_filters(InputStream *ist, OutputStream *ost) | |
380 | { | |
381 | AVFilterContext *last_filter, *filter; | |
382 | /** filter graph containing all filters including input & output */ | |
383 | AVCodecContext *codec = ost->st->codec; | |
384 | AVCodecContext *icodec = ist->st->codec; | |
385 | FFSinkContext ffsink_ctx = { .pix_fmt = codec->pix_fmt }; | |
386 | AVRational sample_aspect_ratio; | |
387 | char args[255]; | |
388 | int ret; | |
389 | ||
390 | ost->graph = avfilter_graph_alloc(); | |
391 | ||
392 | if (ist->st->sample_aspect_ratio.num){ | |
393 | sample_aspect_ratio = ist->st->sample_aspect_ratio; | |
394 | }else | |
395 | sample_aspect_ratio = ist->st->codec->sample_aspect_ratio; | |
396 | ||
397 | snprintf(args, 255, "%d:%d:%d:%d:%d:%d:%d", ist->st->codec->width, | |
398 | ist->st->codec->height, ist->st->codec->pix_fmt, 1, AV_TIME_BASE, | |
399 | sample_aspect_ratio.num, sample_aspect_ratio.den); | |
400 | ||
401 | ret = avfilter_graph_create_filter(&ost->input_video_filter, avfilter_get_by_name("buffer"), | |
402 | "src", args, NULL, ost->graph); | |
403 | if (ret < 0) | |
404 | return ret; | |
405 | ret = avfilter_graph_create_filter(&ost->output_video_filter, &ffsink, | |
406 | "out", NULL, &ffsink_ctx, ost->graph); | |
407 | if (ret < 0) | |
408 | return ret; | |
409 | last_filter = ost->input_video_filter; | |
410 | ||
411 | if (codec->width != icodec->width || codec->height != icodec->height) { | |
412 | snprintf(args, 255, "%d:%d:flags=0x%X", | |
413 | codec->width, | |
414 | codec->height, | |
415 | ost->sws_flags); | |
416 | if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"), | |
417 | NULL, args, NULL, ost->graph)) < 0) | |
418 | return ret; | |
419 | if ((ret = avfilter_link(last_filter, 0, filter, 0)) < 0) | |
420 | return ret; | |
421 | last_filter = filter; | |
422 | } | |
423 | ||
424 | snprintf(args, sizeof(args), "flags=0x%X", ost->sws_flags); | |
425 | ost->graph->scale_sws_opts = av_strdup(args); | |
426 | ||
427 | if (ost->avfilter) { | |
428 | AVFilterInOut *outputs = av_malloc(sizeof(AVFilterInOut)); | |
429 | AVFilterInOut *inputs = av_malloc(sizeof(AVFilterInOut)); | |
430 | ||
431 | outputs->name = av_strdup("in"); | |
432 | outputs->filter_ctx = last_filter; | |
433 | outputs->pad_idx = 0; | |
434 | outputs->next = NULL; | |
435 | ||
436 | inputs->name = av_strdup("out"); | |
437 | inputs->filter_ctx = ost->output_video_filter; | |
438 | inputs->pad_idx = 0; | |
439 | inputs->next = NULL; | |
440 | ||
441 | if ((ret = avfilter_graph_parse(ost->graph, ost->avfilter, inputs, outputs, NULL)) < 0) | |
442 | return ret; | |
443 | av_freep(&ost->avfilter); | |
444 | } else { | |
445 | if ((ret = avfilter_link(last_filter, 0, ost->output_video_filter, 0)) < 0) | |
446 | return ret; | |
447 | } | |
448 | ||
449 | if ((ret = avfilter_graph_config(ost->graph, NULL)) < 0) | |
450 | return ret; | |
451 | ||
452 | codec->width = ost->output_video_filter->inputs[0]->w; | |
453 | codec->height = ost->output_video_filter->inputs[0]->h; | |
454 | codec->sample_aspect_ratio = ost->st->sample_aspect_ratio = | |
bb337b4f | 455 | ost->frame_aspect_ratio ? // overridden by the -aspect cli option |
6291d7e4 AK |
456 | av_d2q(ost->frame_aspect_ratio*codec->height/codec->width, 255) : |
457 | ost->output_video_filter->inputs[0]->sample_aspect_ratio; | |
458 | ||
459 | return 0; | |
460 | } | |
461 | #endif /* CONFIG_AVFILTER */ | |
462 | ||
463 | static void term_exit(void) | |
464 | { | |
465 | av_log(NULL, AV_LOG_QUIET, ""); | |
466 | } | |
467 | ||
468 | static volatile int received_sigterm = 0; | |
469 | static volatile int received_nb_signals = 0; | |
470 | ||
471 | static void | |
472 | sigterm_handler(int sig) | |
473 | { | |
474 | received_sigterm = sig; | |
475 | received_nb_signals++; | |
476 | term_exit(); | |
477 | } | |
478 | ||
479 | static void term_init(void) | |
480 | { | |
481 | signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */ | |
482 | signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */ | |
483 | #ifdef SIGXCPU | |
484 | signal(SIGXCPU, sigterm_handler); | |
485 | #endif | |
486 | } | |
487 | ||
488 | static int decode_interrupt_cb(void) | |
489 | { | |
490 | return received_nb_signals > 1; | |
491 | } | |
492 | ||
dad09ff9 | 493 | void exit_program(int ret) |
6291d7e4 AK |
494 | { |
495 | int i; | |
496 | ||
497 | /* close files */ | |
498 | for(i=0;i<nb_output_files;i++) { | |
af70aa45 | 499 | AVFormatContext *s = output_files[i].ctx; |
6291d7e4 AK |
500 | if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb) |
501 | avio_close(s->pb); | |
502 | avformat_free_context(s); | |
af70aa45 | 503 | av_dict_free(&output_files[i].opts); |
6291d7e4 AK |
504 | } |
505 | for(i=0;i<nb_input_files;i++) { | |
506 | av_close_input_file(input_files[i].ctx); | |
507 | } | |
508 | for (i = 0; i < nb_input_streams; i++) | |
509 | av_dict_free(&input_streams[i].opts); | |
510 | ||
511 | av_free(intra_matrix); | |
512 | av_free(inter_matrix); | |
513 | ||
514 | if (vstats_file) | |
515 | fclose(vstats_file); | |
516 | av_free(vstats_filename); | |
517 | ||
6291d7e4 AK |
518 | av_freep(&input_streams); |
519 | av_freep(&input_files); | |
4288e031 | 520 | av_freep(&output_streams); |
af70aa45 | 521 | av_freep(&output_files); |
6291d7e4 | 522 | |
6291d7e4 AK |
523 | uninit_opts(); |
524 | av_free(audio_buf); | |
525 | av_free(audio_out); | |
526 | allocated_audio_buf_size= allocated_audio_out_size= 0; | |
527 | av_free(samples); | |
528 | ||
529 | #if CONFIG_AVFILTER | |
530 | avfilter_uninit(); | |
531 | #endif | |
532 | ||
533 | if (received_sigterm) { | |
534 | fprintf(stderr, | |
535 | "Received signal %d: terminating.\n", | |
536 | (int) received_sigterm); | |
537 | exit (255); | |
538 | } | |
539 | ||
540 | exit(ret); /* not all OS-es handle main() return value */ | |
6291d7e4 AK |
541 | } |
542 | ||
543 | static void assert_avoptions(AVDictionary *m) | |
544 | { | |
545 | AVDictionaryEntry *t; | |
546 | if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) { | |
547 | av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key); | |
548 | exit_program(1); | |
549 | } | |
550 | } | |
551 | ||
552 | static void assert_codec_experimental(AVCodecContext *c, int encoder) | |
553 | { | |
554 | const char *codec_string = encoder ? "encoder" : "decoder"; | |
555 | AVCodec *codec; | |
556 | if (c->codec->capabilities & CODEC_CAP_EXPERIMENTAL && | |
557 | c->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { | |
558 | av_log(NULL, AV_LOG_ERROR, "%s '%s' is experimental and might produce bad " | |
559 | "results.\nAdd '-strict experimental' if you want to use it.\n", | |
560 | codec_string, c->codec->name); | |
561 | codec = encoder ? avcodec_find_encoder(c->codec->id) : avcodec_find_decoder(c->codec->id); | |
562 | if (!(codec->capabilities & CODEC_CAP_EXPERIMENTAL)) | |
563 | av_log(NULL, AV_LOG_ERROR, "Or use the non experimental %s '%s'.\n", | |
564 | codec_string, codec->name); | |
565 | exit_program(1); | |
566 | } | |
567 | } | |
568 | ||
6291d7e4 AK |
569 | static void choose_sample_fmt(AVStream *st, AVCodec *codec) |
570 | { | |
571 | if(codec && codec->sample_fmts){ | |
572 | const enum AVSampleFormat *p= codec->sample_fmts; | |
573 | for(; *p!=-1; p++){ | |
574 | if(*p == st->codec->sample_fmt) | |
575 | break; | |
576 | } | |
577 | if (*p == -1) { | |
578 | av_log(NULL, AV_LOG_WARNING, | |
579 | "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n", | |
580 | av_get_sample_fmt_name(st->codec->sample_fmt), | |
581 | codec->name, | |
582 | av_get_sample_fmt_name(codec->sample_fmts[0])); | |
583 | st->codec->sample_fmt = codec->sample_fmts[0]; | |
584 | } | |
585 | } | |
586 | } | |
587 | ||
588 | /** | |
589 | * Update the requested input sample format based on the output sample format. | |
590 | * This is currently only used to request float output from decoders which | |
591 | * support multiple sample formats, one of which is AV_SAMPLE_FMT_FLT. | |
592 | * Ideally this will be removed in the future when decoders do not do format | |
593 | * conversion and only output in their native format. | |
594 | */ | |
595 | static void update_sample_fmt(AVCodecContext *dec, AVCodec *dec_codec, | |
596 | AVCodecContext *enc) | |
597 | { | |
598 | /* if sample formats match or a decoder sample format has already been | |
599 | requested, just return */ | |
600 | if (enc->sample_fmt == dec->sample_fmt || | |
601 | dec->request_sample_fmt > AV_SAMPLE_FMT_NONE) | |
602 | return; | |
603 | ||
604 | /* if decoder supports more than one output format */ | |
605 | if (dec_codec && dec_codec->sample_fmts && | |
606 | dec_codec->sample_fmts[0] != AV_SAMPLE_FMT_NONE && | |
607 | dec_codec->sample_fmts[1] != AV_SAMPLE_FMT_NONE) { | |
608 | const enum AVSampleFormat *p; | |
609 | int min_dec = -1, min_inc = -1; | |
610 | ||
611 | /* find a matching sample format in the encoder */ | |
612 | for (p = dec_codec->sample_fmts; *p != AV_SAMPLE_FMT_NONE; p++) { | |
613 | if (*p == enc->sample_fmt) { | |
614 | dec->request_sample_fmt = *p; | |
615 | return; | |
616 | } else if (*p > enc->sample_fmt) { | |
617 | min_inc = FFMIN(min_inc, *p - enc->sample_fmt); | |
618 | } else | |
619 | min_dec = FFMIN(min_dec, enc->sample_fmt - *p); | |
620 | } | |
621 | ||
622 | /* if none match, provide the one that matches quality closest */ | |
623 | dec->request_sample_fmt = min_inc > 0 ? enc->sample_fmt + min_inc : | |
624 | enc->sample_fmt - min_dec; | |
625 | } | |
626 | } | |
627 | ||
628 | static void choose_sample_rate(AVStream *st, AVCodec *codec) | |
629 | { | |
630 | if(codec && codec->supported_samplerates){ | |
631 | const int *p= codec->supported_samplerates; | |
632 | int best=0; | |
633 | int best_dist=INT_MAX; | |
634 | for(; *p; p++){ | |
635 | int dist= abs(st->codec->sample_rate - *p); | |
636 | if(dist < best_dist){ | |
637 | best_dist= dist; | |
638 | best= *p; | |
639 | } | |
640 | } | |
641 | if(best_dist){ | |
642 | av_log(st->codec, AV_LOG_WARNING, "Requested sampling rate unsupported using closest supported (%d)\n", best); | |
643 | } | |
644 | st->codec->sample_rate= best; | |
645 | } | |
646 | } | |
647 | ||
648 | static void choose_pixel_fmt(AVStream *st, AVCodec *codec) | |
649 | { | |
650 | if(codec && codec->pix_fmts){ | |
651 | const enum PixelFormat *p= codec->pix_fmts; | |
652 | if(st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL){ | |
653 | if(st->codec->codec_id==CODEC_ID_MJPEG){ | |
654 | p= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE}; | |
655 | }else if(st->codec->codec_id==CODEC_ID_LJPEG){ | |
656 | p= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_BGRA, PIX_FMT_NONE}; | |
657 | } | |
658 | } | |
659 | for(; *p!=-1; p++){ | |
660 | if(*p == st->codec->pix_fmt) | |
661 | break; | |
662 | } | |
663 | if (*p == -1) { | |
664 | if(st->codec->pix_fmt != PIX_FMT_NONE) | |
665 | av_log(NULL, AV_LOG_WARNING, | |
666 | "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n", | |
667 | av_pix_fmt_descriptors[st->codec->pix_fmt].name, | |
668 | codec->name, | |
669 | av_pix_fmt_descriptors[codec->pix_fmts[0]].name); | |
670 | st->codec->pix_fmt = codec->pix_fmts[0]; | |
671 | } | |
672 | } | |
673 | } | |
674 | ||
6291d7e4 AK |
675 | static double |
676 | get_sync_ipts(const OutputStream *ost) | |
677 | { | |
678 | const InputStream *ist = ost->sync_ist; | |
ea065176 AK |
679 | OutputFile *of = &output_files[ost->file_index]; |
680 | return (double)(ist->pts - of->start_time)/AV_TIME_BASE; | |
6291d7e4 AK |
681 | } |
682 | ||
683 | static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx, AVBitStreamFilterContext *bsfc){ | |
684 | int ret; | |
685 | ||
686 | while(bsfc){ | |
687 | AVPacket new_pkt= *pkt; | |
688 | int a= av_bitstream_filter_filter(bsfc, avctx, NULL, | |
689 | &new_pkt.data, &new_pkt.size, | |
690 | pkt->data, pkt->size, | |
691 | pkt->flags & AV_PKT_FLAG_KEY); | |
692 | if(a>0){ | |
693 | av_free_packet(pkt); | |
694 | new_pkt.destruct= av_destruct_packet; | |
695 | } else if(a<0){ | |
696 | fprintf(stderr, "%s failed for stream %d, codec %s", | |
697 | bsfc->filter->name, pkt->stream_index, | |
698 | avctx->codec ? avctx->codec->name : "copy"); | |
699 | print_error("", a); | |
700 | if (exit_on_error) | |
701 | exit_program(1); | |
702 | } | |
703 | *pkt= new_pkt; | |
704 | ||
705 | bsfc= bsfc->next; | |
706 | } | |
707 | ||
708 | ret= av_interleaved_write_frame(s, pkt); | |
709 | if(ret < 0){ | |
710 | print_error("av_interleaved_write_frame()", ret); | |
711 | exit_program(1); | |
712 | } | |
713 | } | |
714 | ||
6291d7e4 AK |
715 | static void do_audio_out(AVFormatContext *s, |
716 | OutputStream *ost, | |
717 | InputStream *ist, | |
718 | unsigned char *buf, int size) | |
719 | { | |
720 | uint8_t *buftmp; | |
721 | int64_t audio_out_size, audio_buf_size; | |
722 | int64_t allocated_for_size= size; | |
723 | ||
724 | int size_out, frame_bytes, ret, resample_changed; | |
725 | AVCodecContext *enc= ost->st->codec; | |
726 | AVCodecContext *dec= ist->st->codec; | |
727 | int osize = av_get_bytes_per_sample(enc->sample_fmt); | |
728 | int isize = av_get_bytes_per_sample(dec->sample_fmt); | |
729 | const int coded_bps = av_get_bits_per_sample(enc->codec->id); | |
730 | ||
731 | need_realloc: | |
732 | audio_buf_size= (allocated_for_size + isize*dec->channels - 1) / (isize*dec->channels); | |
733 | audio_buf_size= (audio_buf_size*enc->sample_rate + dec->sample_rate) / dec->sample_rate; | |
734 | audio_buf_size= audio_buf_size*2 + 10000; //safety factors for the deprecated resampling API | |
735 | audio_buf_size= FFMAX(audio_buf_size, enc->frame_size); | |
736 | audio_buf_size*= osize*enc->channels; | |
737 | ||
738 | audio_out_size= FFMAX(audio_buf_size, enc->frame_size * osize * enc->channels); | |
739 | if(coded_bps > 8*osize) | |
740 | audio_out_size= audio_out_size * coded_bps / (8*osize); | |
741 | audio_out_size += FF_MIN_BUFFER_SIZE; | |
742 | ||
743 | if(audio_out_size > INT_MAX || audio_buf_size > INT_MAX){ | |
744 | fprintf(stderr, "Buffer sizes too large\n"); | |
745 | exit_program(1); | |
746 | } | |
747 | ||
748 | av_fast_malloc(&audio_buf, &allocated_audio_buf_size, audio_buf_size); | |
749 | av_fast_malloc(&audio_out, &allocated_audio_out_size, audio_out_size); | |
750 | if (!audio_buf || !audio_out){ | |
751 | fprintf(stderr, "Out of memory in do_audio_out\n"); | |
752 | exit_program(1); | |
753 | } | |
754 | ||
755 | if (enc->channels != dec->channels || enc->sample_rate != dec->sample_rate) | |
756 | ost->audio_resample = 1; | |
757 | ||
758 | resample_changed = ost->resample_sample_fmt != dec->sample_fmt || | |
759 | ost->resample_channels != dec->channels || | |
760 | ost->resample_sample_rate != dec->sample_rate; | |
761 | ||
762 | if ((ost->audio_resample && !ost->resample) || resample_changed) { | |
763 | if (resample_changed) { | |
764 | av_log(NULL, AV_LOG_INFO, "Input stream #%d.%d frame changed from rate:%d fmt:%s ch:%d to rate:%d fmt:%s ch:%d\n", | |
765 | ist->file_index, ist->st->index, | |
766 | ost->resample_sample_rate, av_get_sample_fmt_name(ost->resample_sample_fmt), ost->resample_channels, | |
767 | dec->sample_rate, av_get_sample_fmt_name(dec->sample_fmt), dec->channels); | |
768 | ost->resample_sample_fmt = dec->sample_fmt; | |
769 | ost->resample_channels = dec->channels; | |
770 | ost->resample_sample_rate = dec->sample_rate; | |
771 | if (ost->resample) | |
772 | audio_resample_close(ost->resample); | |
773 | } | |
774 | /* if audio_sync_method is >1 the resampler is needed for audio drift compensation */ | |
775 | if (audio_sync_method <= 1 && | |
776 | ost->resample_sample_fmt == enc->sample_fmt && | |
777 | ost->resample_channels == enc->channels && | |
778 | ost->resample_sample_rate == enc->sample_rate) { | |
779 | ost->resample = NULL; | |
780 | ost->audio_resample = 0; | |
781 | } else if (ost->audio_resample) { | |
782 | if (dec->sample_fmt != AV_SAMPLE_FMT_S16) | |
783 | fprintf(stderr, "Warning, using s16 intermediate sample format for resampling\n"); | |
784 | ost->resample = av_audio_resample_init(enc->channels, dec->channels, | |
785 | enc->sample_rate, dec->sample_rate, | |
786 | enc->sample_fmt, dec->sample_fmt, | |
787 | 16, 10, 0, 0.8); | |
788 | if (!ost->resample) { | |
789 | fprintf(stderr, "Can not resample %d channels @ %d Hz to %d channels @ %d Hz\n", | |
790 | dec->channels, dec->sample_rate, | |
791 | enc->channels, enc->sample_rate); | |
792 | exit_program(1); | |
793 | } | |
794 | } | |
795 | } | |
796 | ||
797 | #define MAKE_SFMT_PAIR(a,b) ((a)+AV_SAMPLE_FMT_NB*(b)) | |
798 | if (!ost->audio_resample && dec->sample_fmt!=enc->sample_fmt && | |
799 | MAKE_SFMT_PAIR(enc->sample_fmt,dec->sample_fmt)!=ost->reformat_pair) { | |
800 | if (ost->reformat_ctx) | |
801 | av_audio_convert_free(ost->reformat_ctx); | |
802 | ost->reformat_ctx = av_audio_convert_alloc(enc->sample_fmt, 1, | |
803 | dec->sample_fmt, 1, NULL, 0); | |
804 | if (!ost->reformat_ctx) { | |
805 | fprintf(stderr, "Cannot convert %s sample format to %s sample format\n", | |
806 | av_get_sample_fmt_name(dec->sample_fmt), | |
807 | av_get_sample_fmt_name(enc->sample_fmt)); | |
808 | exit_program(1); | |
809 | } | |
810 | ost->reformat_pair=MAKE_SFMT_PAIR(enc->sample_fmt,dec->sample_fmt); | |
811 | } | |
812 | ||
813 | if(audio_sync_method){ | |
814 | double delta = get_sync_ipts(ost) * enc->sample_rate - ost->sync_opts | |
815 | - av_fifo_size(ost->fifo)/(enc->channels * 2); | |
816 | double idelta= delta*dec->sample_rate / enc->sample_rate; | |
817 | int byte_delta= ((int)idelta)*2*dec->channels; | |
818 | ||
819 | //FIXME resample delay | |
820 | if(fabs(delta) > 50){ | |
821 | if(ist->is_start || fabs(delta) > audio_drift_threshold*enc->sample_rate){ | |
822 | if(byte_delta < 0){ | |
823 | byte_delta= FFMAX(byte_delta, -size); | |
824 | size += byte_delta; | |
825 | buf -= byte_delta; | |
826 | if(verbose > 2) | |
827 | fprintf(stderr, "discarding %d audio samples\n", (int)-delta); | |
828 | if(!size) | |
829 | return; | |
830 | ist->is_start=0; | |
831 | }else{ | |
832 | static uint8_t *input_tmp= NULL; | |
833 | input_tmp= av_realloc(input_tmp, byte_delta + size); | |
834 | ||
835 | if(byte_delta > allocated_for_size - size){ | |
836 | allocated_for_size= byte_delta + (int64_t)size; | |
837 | goto need_realloc; | |
838 | } | |
839 | ist->is_start=0; | |
840 | ||
841 | memset(input_tmp, 0, byte_delta); | |
842 | memcpy(input_tmp + byte_delta, buf, size); | |
843 | buf= input_tmp; | |
844 | size += byte_delta; | |
845 | if(verbose > 2) | |
846 | fprintf(stderr, "adding %d audio samples of silence\n", (int)delta); | |
847 | } | |
848 | }else if(audio_sync_method>1){ | |
849 | int comp= av_clip(delta, -audio_sync_method, audio_sync_method); | |
850 | av_assert0(ost->audio_resample); | |
851 | if(verbose > 2) | |
852 | fprintf(stderr, "compensating audio timestamp drift:%f compensation:%d in:%d\n", delta, comp, enc->sample_rate); | |
853 | // fprintf(stderr, "drift:%f len:%d opts:%"PRId64" ipts:%"PRId64" fifo:%d\n", delta, -1, ost->sync_opts, (int64_t)(get_sync_ipts(ost) * enc->sample_rate), av_fifo_size(ost->fifo)/(ost->st->codec->channels * 2)); | |
854 | av_resample_compensate(*(struct AVResampleContext**)ost->resample, comp, enc->sample_rate); | |
855 | } | |
856 | } | |
857 | }else | |
858 | ost->sync_opts= lrintf(get_sync_ipts(ost) * enc->sample_rate) | |
859 | - av_fifo_size(ost->fifo)/(enc->channels * 2); //FIXME wrong | |
860 | ||
861 | if (ost->audio_resample) { | |
862 | buftmp = audio_buf; | |
863 | size_out = audio_resample(ost->resample, | |
864 | (short *)buftmp, (short *)buf, | |
865 | size / (dec->channels * isize)); | |
866 | size_out = size_out * enc->channels * osize; | |
867 | } else { | |
868 | buftmp = buf; | |
869 | size_out = size; | |
870 | } | |
871 | ||
872 | if (!ost->audio_resample && dec->sample_fmt!=enc->sample_fmt) { | |
873 | const void *ibuf[6]= {buftmp}; | |
874 | void *obuf[6]= {audio_buf}; | |
875 | int istride[6]= {isize}; | |
876 | int ostride[6]= {osize}; | |
877 | int len= size_out/istride[0]; | |
878 | if (av_audio_convert(ost->reformat_ctx, obuf, ostride, ibuf, istride, len)<0) { | |
879 | printf("av_audio_convert() failed\n"); | |
880 | if (exit_on_error) | |
881 | exit_program(1); | |
882 | return; | |
883 | } | |
884 | buftmp = audio_buf; | |
885 | size_out = len*osize; | |
886 | } | |
887 | ||
888 | /* now encode as many frames as possible */ | |
889 | if (enc->frame_size > 1) { | |
890 | /* output resampled raw samples */ | |
891 | if (av_fifo_realloc2(ost->fifo, av_fifo_size(ost->fifo) + size_out) < 0) { | |
892 | fprintf(stderr, "av_fifo_realloc2() failed\n"); | |
893 | exit_program(1); | |
894 | } | |
895 | av_fifo_generic_write(ost->fifo, buftmp, size_out, NULL); | |
896 | ||
897 | frame_bytes = enc->frame_size * osize * enc->channels; | |
898 | ||
899 | while (av_fifo_size(ost->fifo) >= frame_bytes) { | |
900 | AVPacket pkt; | |
901 | av_init_packet(&pkt); | |
902 | ||
903 | av_fifo_generic_read(ost->fifo, audio_buf, frame_bytes, NULL); | |
904 | ||
905 | //FIXME pass ost->sync_opts as AVFrame.pts in avcodec_encode_audio() | |
906 | ||
907 | ret = avcodec_encode_audio(enc, audio_out, audio_out_size, | |
908 | (short *)audio_buf); | |
909 | if (ret < 0) { | |
910 | fprintf(stderr, "Audio encoding failed\n"); | |
911 | exit_program(1); | |
912 | } | |
913 | audio_size += ret; | |
914 | pkt.stream_index= ost->index; | |
915 | pkt.data= audio_out; | |
916 | pkt.size= ret; | |
917 | if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE) | |
918 | pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base); | |
919 | pkt.flags |= AV_PKT_FLAG_KEY; | |
920 | write_frame(s, &pkt, enc, ost->bitstream_filters); | |
921 | ||
922 | ost->sync_opts += enc->frame_size; | |
923 | } | |
924 | } else { | |
925 | AVPacket pkt; | |
926 | av_init_packet(&pkt); | |
927 | ||
928 | ost->sync_opts += size_out / (osize * enc->channels); | |
929 | ||
930 | /* output a pcm frame */ | |
931 | /* determine the size of the coded buffer */ | |
932 | size_out /= osize; | |
933 | if (coded_bps) | |
934 | size_out = size_out*coded_bps/8; | |
935 | ||
936 | if(size_out > audio_out_size){ | |
937 | fprintf(stderr, "Internal error, buffer size too small\n"); | |
938 | exit_program(1); | |
939 | } | |
940 | ||
941 | //FIXME pass ost->sync_opts as AVFrame.pts in avcodec_encode_audio() | |
942 | ret = avcodec_encode_audio(enc, audio_out, size_out, | |
943 | (short *)buftmp); | |
944 | if (ret < 0) { | |
945 | fprintf(stderr, "Audio encoding failed\n"); | |
946 | exit_program(1); | |
947 | } | |
948 | audio_size += ret; | |
949 | pkt.stream_index= ost->index; | |
950 | pkt.data= audio_out; | |
951 | pkt.size= ret; | |
952 | if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE) | |
953 | pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base); | |
954 | pkt.flags |= AV_PKT_FLAG_KEY; | |
955 | write_frame(s, &pkt, enc, ost->bitstream_filters); | |
956 | } | |
957 | } | |
958 | ||
959 | static void pre_process_video_frame(InputStream *ist, AVPicture *picture, void **bufp) | |
960 | { | |
961 | AVCodecContext *dec; | |
962 | AVPicture *picture2; | |
963 | AVPicture picture_tmp; | |
964 | uint8_t *buf = 0; | |
965 | ||
966 | dec = ist->st->codec; | |
967 | ||
968 | /* deinterlace : must be done before any resize */ | |
969 | if (do_deinterlace) { | |
970 | int size; | |
971 | ||
972 | /* create temporary picture */ | |
973 | size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height); | |
974 | buf = av_malloc(size); | |
975 | if (!buf) | |
976 | return; | |
977 | ||
978 | picture2 = &picture_tmp; | |
979 | avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height); | |
980 | ||
981 | if(avpicture_deinterlace(picture2, picture, | |
982 | dec->pix_fmt, dec->width, dec->height) < 0) { | |
983 | /* if error, do not deinterlace */ | |
984 | fprintf(stderr, "Deinterlacing failed\n"); | |
985 | av_free(buf); | |
986 | buf = NULL; | |
987 | picture2 = picture; | |
988 | } | |
989 | } else { | |
990 | picture2 = picture; | |
991 | } | |
992 | ||
993 | if (picture != picture2) | |
994 | *picture = *picture2; | |
995 | *bufp = buf; | |
996 | } | |
997 | ||
6291d7e4 AK |
998 | static void do_subtitle_out(AVFormatContext *s, |
999 | OutputStream *ost, | |
1000 | InputStream *ist, | |
1001 | AVSubtitle *sub, | |
1002 | int64_t pts) | |
1003 | { | |
1004 | static uint8_t *subtitle_out = NULL; | |
1005 | int subtitle_out_max_size = 1024 * 1024; | |
1006 | int subtitle_out_size, nb, i; | |
1007 | AVCodecContext *enc; | |
1008 | AVPacket pkt; | |
1009 | ||
1010 | if (pts == AV_NOPTS_VALUE) { | |
1011 | fprintf(stderr, "Subtitle packets must have a pts\n"); | |
1012 | if (exit_on_error) | |
1013 | exit_program(1); | |
1014 | return; | |
1015 | } | |
1016 | ||
1017 | enc = ost->st->codec; | |
1018 | ||
1019 | if (!subtitle_out) { | |
1020 | subtitle_out = av_malloc(subtitle_out_max_size); | |
1021 | } | |
1022 | ||
1023 | /* Note: DVB subtitle need one packet to draw them and one other | |
1024 | packet to clear them */ | |
1025 | /* XXX: signal it in the codec context ? */ | |
1026 | if (enc->codec_id == CODEC_ID_DVB_SUBTITLE) | |
1027 | nb = 2; | |
1028 | else | |
1029 | nb = 1; | |
1030 | ||
1031 | for(i = 0; i < nb; i++) { | |
1032 | sub->pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q); | |
1033 | // start_display_time is required to be 0 | |
1034 | sub->pts += av_rescale_q(sub->start_display_time, (AVRational){1, 1000}, AV_TIME_BASE_Q); | |
1035 | sub->end_display_time -= sub->start_display_time; | |
1036 | sub->start_display_time = 0; | |
1037 | subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out, | |
1038 | subtitle_out_max_size, sub); | |
1039 | if (subtitle_out_size < 0) { | |
1040 | fprintf(stderr, "Subtitle encoding failed\n"); | |
1041 | exit_program(1); | |
1042 | } | |
1043 | ||
1044 | av_init_packet(&pkt); | |
1045 | pkt.stream_index = ost->index; | |
1046 | pkt.data = subtitle_out; | |
1047 | pkt.size = subtitle_out_size; | |
1048 | pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base); | |
1049 | if (enc->codec_id == CODEC_ID_DVB_SUBTITLE) { | |
1050 | /* XXX: the pts correction is handled here. Maybe handling | |
1051 | it in the codec would be better */ | |
1052 | if (i == 0) | |
1053 | pkt.pts += 90 * sub->start_display_time; | |
1054 | else | |
1055 | pkt.pts += 90 * sub->end_display_time; | |
1056 | } | |
1057 | write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters); | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | static int bit_buffer_size= 1024*256; | |
1062 | static uint8_t *bit_buffer= NULL; | |
1063 | ||
87ef060c AC |
1064 | static void do_video_resample(OutputStream *ost, |
1065 | InputStream *ist, | |
1066 | AVFrame *in_picture, | |
1067 | AVFrame **out_picture) | |
6291d7e4 | 1068 | { |
87ef060c AC |
1069 | int resample_changed = 0; |
1070 | AVCodecContext *dec = ist->st->codec; | |
1071 | *out_picture = in_picture; | |
6291d7e4 AK |
1072 | |
1073 | resample_changed = ost->resample_width != dec->width || | |
1074 | ost->resample_height != dec->height || | |
1075 | ost->resample_pix_fmt != dec->pix_fmt; | |
1076 | ||
1077 | if (resample_changed) { | |
1078 | av_log(NULL, AV_LOG_INFO, | |
1079 | "Input stream #%d.%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n", | |
1080 | ist->file_index, ist->st->index, | |
1081 | ost->resample_width, ost->resample_height, av_get_pix_fmt_name(ost->resample_pix_fmt), | |
1082 | dec->width , dec->height , av_get_pix_fmt_name(dec->pix_fmt)); | |
1083 | if(!ost->video_resample) | |
5c59fa56 | 1084 | ost->video_resample = 1; |
6291d7e4 AK |
1085 | } |
1086 | ||
1087 | #if !CONFIG_AVFILTER | |
1088 | if (ost->video_resample) { | |
87ef060c | 1089 | *out_picture = &ost->pict_tmp; |
6291d7e4 AK |
1090 | if (resample_changed) { |
1091 | /* initialize a new scaler context */ | |
1092 | sws_freeContext(ost->img_resample_ctx); | |
1093 | ost->img_resample_ctx = sws_getContext( | |
1094 | ist->st->codec->width, | |
1095 | ist->st->codec->height, | |
1096 | ist->st->codec->pix_fmt, | |
1097 | ost->st->codec->width, | |
1098 | ost->st->codec->height, | |
1099 | ost->st->codec->pix_fmt, | |
1100 | ost->sws_flags, NULL, NULL, NULL); | |
1101 | if (ost->img_resample_ctx == NULL) { | |
1102 | fprintf(stderr, "Cannot get resampling context\n"); | |
1103 | exit_program(1); | |
1104 | } | |
1105 | } | |
87ef060c AC |
1106 | sws_scale(ost->img_resample_ctx, in_picture->data, in_picture->linesize, |
1107 | 0, ost->resample_height, (*out_picture)->data, (*out_picture)->linesize); | |
6291d7e4 | 1108 | } |
428c59d9 K |
1109 | #else |
1110 | if (resample_changed) { | |
1111 | avfilter_graph_free(&ost->graph); | |
1112 | if (configure_video_filters(ist, ost)) { | |
bb337b4f | 1113 | fprintf(stderr, "Error reinitializing filters!\n"); |
428c59d9 K |
1114 | exit_program(1); |
1115 | } | |
1116 | } | |
6291d7e4 | 1117 | #endif |
428c59d9 K |
1118 | if (resample_changed) { |
1119 | ost->resample_width = dec->width; | |
1120 | ost->resample_height = dec->height; | |
1121 | ost->resample_pix_fmt = dec->pix_fmt; | |
1122 | } | |
87ef060c AC |
1123 | } |
1124 | ||
1125 | ||
1126 | static void do_video_out(AVFormatContext *s, | |
1127 | OutputStream *ost, | |
1128 | InputStream *ist, | |
1129 | AVFrame *in_picture, | |
1130 | int *frame_size, float quality) | |
1131 | { | |
553735f5 | 1132 | int nb_frames, i, ret, format_video_sync; |
87ef060c | 1133 | AVFrame *final_picture; |
f593628e | 1134 | AVCodecContext *enc; |
87ef060c AC |
1135 | double sync_ipts; |
1136 | ||
1137 | enc = ost->st->codec; | |
87ef060c AC |
1138 | |
1139 | sync_ipts = get_sync_ipts(ost) / av_q2d(enc->time_base); | |
1140 | ||
1141 | /* by default, we output a single frame */ | |
1142 | nb_frames = 1; | |
1143 | ||
1144 | *frame_size = 0; | |
1145 | ||
553735f5 AC |
1146 | format_video_sync = video_sync_method; |
1147 | if (format_video_sync < 0) | |
1148 | format_video_sync = (s->oformat->flags & AVFMT_VARIABLE_FPS) ? 2 : 1; | |
1149 | ||
1150 | if (format_video_sync) { | |
87ef060c AC |
1151 | double vdelta = sync_ipts - ost->sync_opts; |
1152 | //FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c | |
1153 | if (vdelta < -1.1) | |
1154 | nb_frames = 0; | |
553735f5 | 1155 | else if (format_video_sync == 2) { |
87ef060c AC |
1156 | if(vdelta<=-0.6){ |
1157 | nb_frames=0; | |
1158 | }else if(vdelta>0.6) | |
1159 | ost->sync_opts= lrintf(sync_ipts); | |
1160 | }else if (vdelta > 1.1) | |
1161 | nb_frames = lrintf(vdelta); | |
1162 | //fprintf(stderr, "vdelta:%f, ost->sync_opts:%"PRId64", ost->sync_ipts:%f nb_frames:%d\n", vdelta, ost->sync_opts, get_sync_ipts(ost), nb_frames); | |
1163 | if (nb_frames == 0){ | |
1164 | ++nb_frames_drop; | |
1165 | if (verbose>2) | |
1166 | fprintf(stderr, "*** drop!\n"); | |
1167 | }else if (nb_frames > 1) { | |
1168 | nb_frames_dup += nb_frames - 1; | |
1169 | if (verbose>2) | |
1170 | fprintf(stderr, "*** %d dup!\n", nb_frames-1); | |
1171 | } | |
1172 | }else | |
1173 | ost->sync_opts= lrintf(sync_ipts); | |
1174 | ||
96139b5e | 1175 | nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number); |
87ef060c AC |
1176 | if (nb_frames <= 0) |
1177 | return; | |
1178 | ||
1179 | do_video_resample(ost, ist, in_picture, &final_picture); | |
6291d7e4 AK |
1180 | |
1181 | /* duplicates frame if needed */ | |
1182 | for(i=0;i<nb_frames;i++) { | |
1183 | AVPacket pkt; | |
1184 | av_init_packet(&pkt); | |
1185 | pkt.stream_index= ost->index; | |
1186 | ||
1187 | if (s->oformat->flags & AVFMT_RAWPICTURE) { | |
1188 | /* raw pictures are written as AVPicture structure to | |
bb337b4f | 1189 | avoid any copies. We support temporarily the older |
6291d7e4 | 1190 | method. */ |
f593628e AC |
1191 | enc->coded_frame->interlaced_frame = in_picture->interlaced_frame; |
1192 | enc->coded_frame->top_field_first = in_picture->top_field_first; | |
6291d7e4 AK |
1193 | pkt.data= (uint8_t *)final_picture; |
1194 | pkt.size= sizeof(AVPicture); | |
1195 | pkt.pts= av_rescale_q(ost->sync_opts, enc->time_base, ost->st->time_base); | |
1196 | pkt.flags |= AV_PKT_FLAG_KEY; | |
1197 | ||
1198 | write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters); | |
6291d7e4 AK |
1199 | } else { |
1200 | AVFrame big_picture; | |
1201 | ||
1202 | big_picture= *final_picture; | |
1203 | /* better than nothing: use input picture interlaced | |
1204 | settings */ | |
1205 | big_picture.interlaced_frame = in_picture->interlaced_frame; | |
1206 | if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) { | |
1207 | if(top_field_first == -1) | |
1208 | big_picture.top_field_first = in_picture->top_field_first; | |
1209 | else | |
1210 | big_picture.top_field_first = top_field_first; | |
1211 | } | |
1212 | ||
f4ad238c | 1213 | /* handles same_quant here. This is not correct because it may |
6291d7e4 AK |
1214 | not be a global option */ |
1215 | big_picture.quality = quality; | |
1216 | if(!me_threshold) | |
1217 | big_picture.pict_type = 0; | |
1218 | // big_picture.pts = AV_NOPTS_VALUE; | |
1219 | big_picture.pts= ost->sync_opts; | |
1220 | // big_picture.pts= av_rescale(ost->sync_opts, AV_TIME_BASE*(int64_t)enc->time_base.num, enc->time_base.den); | |
1221 | //av_log(NULL, AV_LOG_DEBUG, "%"PRId64" -> encoder\n", ost->sync_opts); | |
1222 | if (ost->forced_kf_index < ost->forced_kf_count && | |
1223 | big_picture.pts >= ost->forced_kf_pts[ost->forced_kf_index]) { | |
1224 | big_picture.pict_type = AV_PICTURE_TYPE_I; | |
1225 | ost->forced_kf_index++; | |
1226 | } | |
1227 | ret = avcodec_encode_video(enc, | |
1228 | bit_buffer, bit_buffer_size, | |
1229 | &big_picture); | |
1230 | if (ret < 0) { | |
1231 | fprintf(stderr, "Video encoding failed\n"); | |
1232 | exit_program(1); | |
1233 | } | |
1234 | ||
1235 | if(ret>0){ | |
1236 | pkt.data= bit_buffer; | |
1237 | pkt.size= ret; | |
1238 | if(enc->coded_frame->pts != AV_NOPTS_VALUE) | |
1239 | pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base); | |
1240 | /*av_log(NULL, AV_LOG_DEBUG, "encoder -> %"PRId64"/%"PRId64"\n", | |
1241 | pkt.pts != AV_NOPTS_VALUE ? av_rescale(pkt.pts, enc->time_base.den, AV_TIME_BASE*(int64_t)enc->time_base.num) : -1, | |
1242 | pkt.dts != AV_NOPTS_VALUE ? av_rescale(pkt.dts, enc->time_base.den, AV_TIME_BASE*(int64_t)enc->time_base.num) : -1);*/ | |
1243 | ||
1244 | if(enc->coded_frame->key_frame) | |
1245 | pkt.flags |= AV_PKT_FLAG_KEY; | |
1246 | write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters); | |
1247 | *frame_size = ret; | |
1248 | video_size += ret; | |
1249 | //fprintf(stderr,"\nFrame: %3d size: %5d type: %d", | |
1250 | // enc->frame_number-1, ret, enc->pict_type); | |
1251 | /* if two pass, output log */ | |
1252 | if (ost->logfile && enc->stats_out) { | |
1253 | fprintf(ost->logfile, "%s", enc->stats_out); | |
1254 | } | |
1255 | } | |
1256 | } | |
1257 | ost->sync_opts++; | |
1258 | ost->frame_number++; | |
1259 | } | |
1260 | } | |
1261 | ||
1262 | static double psnr(double d){ | |
1263 | return -10.0*log(d)/log(10.0); | |
1264 | } | |
1265 | ||
1266 | static void do_video_stats(AVFormatContext *os, OutputStream *ost, | |
1267 | int frame_size) | |
1268 | { | |
1269 | AVCodecContext *enc; | |
1270 | int frame_number; | |
1271 | double ti1, bitrate, avg_bitrate; | |
1272 | ||
1273 | /* this is executed just the first time do_video_stats is called */ | |
1274 | if (!vstats_file) { | |
1275 | vstats_file = fopen(vstats_filename, "w"); | |
1276 | if (!vstats_file) { | |
1277 | perror("fopen"); | |
1278 | exit_program(1); | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | enc = ost->st->codec; | |
1283 | if (enc->codec_type == AVMEDIA_TYPE_VIDEO) { | |
1284 | frame_number = ost->frame_number; | |
1285 | fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA); | |
1286 | if (enc->flags&CODEC_FLAG_PSNR) | |
1287 | fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0))); | |
1288 | ||
1289 | fprintf(vstats_file,"f_size= %6d ", frame_size); | |
1290 | /* compute pts value */ | |
1291 | ti1 = ost->sync_opts * av_q2d(enc->time_base); | |
1292 | if (ti1 < 0.01) | |
1293 | ti1 = 0.01; | |
1294 | ||
1295 | bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0; | |
1296 | avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0; | |
1297 | fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ", | |
1298 | (double)video_size / 1024, ti1, bitrate, avg_bitrate); | |
1299 | fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type)); | |
1300 | } | |
1301 | } | |
1302 | ||
af70aa45 | 1303 | static void print_report(OutputFile *output_files, |
4288e031 | 1304 | OutputStream *ost_table, int nb_ostreams, |
c5ad2c2c | 1305 | int is_last_report, int64_t timer_start) |
6291d7e4 AK |
1306 | { |
1307 | char buf[1024]; | |
1308 | OutputStream *ost; | |
1309 | AVFormatContext *oc; | |
1310 | int64_t total_size; | |
1311 | AVCodecContext *enc; | |
1312 | int frame_number, vid, i; | |
1313 | double bitrate, ti1, pts; | |
1314 | static int64_t last_time = -1; | |
1315 | static int qp_histogram[52]; | |
1316 | ||
1317 | if (!is_last_report) { | |
1318 | int64_t cur_time; | |
1319 | /* display the report every 0.5 seconds */ | |
1320 | cur_time = av_gettime(); | |
1321 | if (last_time == -1) { | |
1322 | last_time = cur_time; | |
1323 | return; | |
1324 | } | |
1325 | if ((cur_time - last_time) < 500000) | |
1326 | return; | |
1327 | last_time = cur_time; | |
1328 | } | |
1329 | ||
1330 | ||
af70aa45 | 1331 | oc = output_files[0].ctx; |
6291d7e4 AK |
1332 | |
1333 | total_size = avio_size(oc->pb); | |
1334 | if(total_size<0) // FIXME improve avio_size() so it works with non seekable output too | |
1335 | total_size= avio_tell(oc->pb); | |
1336 | ||
1337 | buf[0] = '\0'; | |
1338 | ti1 = 1e10; | |
1339 | vid = 0; | |
1340 | for(i=0;i<nb_ostreams;i++) { | |
1341 | float q = -1; | |
4288e031 | 1342 | ost = &ost_table[i]; |
6291d7e4 AK |
1343 | enc = ost->st->codec; |
1344 | if (!ost->st->stream_copy && enc->coded_frame) | |
1345 | q = enc->coded_frame->quality/(float)FF_QP2LAMBDA; | |
1346 | if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) { | |
1347 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q); | |
1348 | } | |
1349 | if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) { | |
1350 | float t = (av_gettime()-timer_start) / 1000000.0; | |
1351 | ||
1352 | frame_number = ost->frame_number; | |
1353 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3d q=%3.1f ", | |
1354 | frame_number, (t>1)?(int)(frame_number/t+0.5) : 0, q); | |
1355 | if(is_last_report) | |
1356 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L"); | |
1357 | if(qp_hist){ | |
1358 | int j; | |
1359 | int qp = lrintf(q); | |
1360 | if(qp>=0 && qp<FF_ARRAY_ELEMS(qp_histogram)) | |
1361 | qp_histogram[qp]++; | |
1362 | for(j=0; j<32; j++) | |
1363 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log(qp_histogram[j]+1)/log(2))); | |
1364 | } | |
1365 | if (enc->flags&CODEC_FLAG_PSNR){ | |
1366 | int j; | |
1367 | double error, error_sum=0; | |
1368 | double scale, scale_sum=0; | |
1369 | char type[3]= {'Y','U','V'}; | |
1370 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR="); | |
1371 | for(j=0; j<3; j++){ | |
1372 | if(is_last_report){ | |
1373 | error= enc->error[j]; | |
1374 | scale= enc->width*enc->height*255.0*255.0*frame_number; | |
1375 | }else{ | |
1376 | error= enc->coded_frame->error[j]; | |
1377 | scale= enc->width*enc->height*255.0*255.0; | |
1378 | } | |
1379 | if(j) scale/=4; | |
1380 | error_sum += error; | |
1381 | scale_sum += scale; | |
1382 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error/scale)); | |
1383 | } | |
1384 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum/scale_sum)); | |
1385 | } | |
1386 | vid = 1; | |
1387 | } | |
1388 | /* compute min output value */ | |
1389 | pts = (double)ost->st->pts.val * av_q2d(ost->st->time_base); | |
1390 | if ((pts < ti1) && (pts > 0)) | |
1391 | ti1 = pts; | |
1392 | } | |
1393 | if (ti1 < 0.01) | |
1394 | ti1 = 0.01; | |
1395 | ||
1396 | if (verbose > 0 || is_last_report) { | |
1397 | bitrate = (double)(total_size * 8) / ti1 / 1000.0; | |
1398 | ||
1399 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
1400 | "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s", | |
1401 | (double)total_size / 1024, ti1, bitrate); | |
1402 | ||
1403 | if (nb_frames_dup || nb_frames_drop) | |
1404 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d", | |
1405 | nb_frames_dup, nb_frames_drop); | |
1406 | ||
1407 | if (verbose >= 0) | |
1408 | fprintf(stderr, "%s \r", buf); | |
1409 | ||
1410 | fflush(stderr); | |
1411 | } | |
1412 | ||
1413 | if (is_last_report && verbose >= 0){ | |
1414 | int64_t raw= audio_size + video_size + extra_size; | |
1415 | fprintf(stderr, "\n"); | |
1416 | fprintf(stderr, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n", | |
1417 | video_size/1024.0, | |
1418 | audio_size/1024.0, | |
1419 | extra_size/1024.0, | |
1420 | 100.0*(total_size - raw)/raw | |
1421 | ); | |
1422 | } | |
1423 | } | |
1424 | ||
1425 | static void generate_silence(uint8_t* buf, enum AVSampleFormat sample_fmt, size_t size) | |
1426 | { | |
1427 | int fill_char = 0x00; | |
1428 | if (sample_fmt == AV_SAMPLE_FMT_U8) | |
1429 | fill_char = 0x80; | |
1430 | memset(buf, fill_char, size); | |
1431 | } | |
1432 | ||
b62b5cb6 | 1433 | static void flush_encoders(OutputStream *ost_table, int nb_ostreams) |
4a4ce2e7 AK |
1434 | { |
1435 | int i, ret; | |
1436 | ||
1437 | for (i = 0; i < nb_ostreams; i++) { | |
6f1c66d5 AK |
1438 | OutputStream *ost = &ost_table[i]; |
1439 | AVCodecContext *enc = ost->st->codec; | |
1440 | AVFormatContext *os = output_files[ost->file_index].ctx; | |
1441 | ||
b62b5cb6 | 1442 | if (!ost->encoding_needed) |
6f1c66d5 AK |
1443 | continue; |
1444 | ||
1445 | if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <=1) | |
1446 | continue; | |
1447 | if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE)) | |
1448 | continue; | |
1449 | ||
1450 | for(;;) { | |
1451 | AVPacket pkt; | |
1452 | int fifo_bytes; | |
1453 | av_init_packet(&pkt); | |
1454 | pkt.stream_index= ost->index; | |
1455 | ||
1456 | switch (ost->st->codec->codec_type) { | |
1457 | case AVMEDIA_TYPE_AUDIO: | |
1458 | fifo_bytes = av_fifo_size(ost->fifo); | |
1459 | ret = 0; | |
1460 | /* encode any samples remaining in fifo */ | |
1461 | if (fifo_bytes > 0) { | |
1462 | int osize = av_get_bytes_per_sample(enc->sample_fmt); | |
1463 | int fs_tmp = enc->frame_size; | |
1464 | ||
1465 | av_fifo_generic_read(ost->fifo, audio_buf, fifo_bytes, NULL); | |
1466 | if (enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) { | |
1467 | enc->frame_size = fifo_bytes / (osize * enc->channels); | |
1468 | } else { /* pad */ | |
1469 | int frame_bytes = enc->frame_size*osize*enc->channels; | |
1470 | if (allocated_audio_buf_size < frame_bytes) | |
4a4ce2e7 | 1471 | exit_program(1); |
6f1c66d5 | 1472 | generate_silence(audio_buf+fifo_bytes, enc->sample_fmt, frame_bytes - fifo_bytes); |
4a4ce2e7 AK |
1473 | } |
1474 | ||
6f1c66d5 AK |
1475 | ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, (short *)audio_buf); |
1476 | pkt.duration = av_rescale((int64_t)enc->frame_size*ost->st->time_base.den, | |
1477 | ost->st->time_base.num, enc->sample_rate); | |
1478 | enc->frame_size = fs_tmp; | |
1479 | } | |
1480 | if (ret <= 0) { | |
1481 | ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL); | |
4a4ce2e7 | 1482 | } |
6f1c66d5 AK |
1483 | if (ret < 0) { |
1484 | fprintf(stderr, "Audio encoding failed\n"); | |
1485 | exit_program(1); | |
1486 | } | |
1487 | audio_size += ret; | |
1488 | pkt.flags |= AV_PKT_FLAG_KEY; | |
1489 | break; | |
1490 | case AVMEDIA_TYPE_VIDEO: | |
1491 | ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL); | |
1492 | if (ret < 0) { | |
1493 | fprintf(stderr, "Video encoding failed\n"); | |
1494 | exit_program(1); | |
1495 | } | |
1496 | video_size += ret; | |
1497 | if(enc->coded_frame && enc->coded_frame->key_frame) | |
1498 | pkt.flags |= AV_PKT_FLAG_KEY; | |
1499 | if (ost->logfile && enc->stats_out) { | |
1500 | fprintf(ost->logfile, "%s", enc->stats_out); | |
1501 | } | |
1502 | break; | |
1503 | default: | |
1504 | ret=-1; | |
4a4ce2e7 | 1505 | } |
6f1c66d5 AK |
1506 | |
1507 | if (ret <= 0) | |
1508 | break; | |
1509 | pkt.data = bit_buffer; | |
1510 | pkt.size = ret; | |
1511 | if (enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE) | |
1512 | pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base); | |
1513 | write_frame(os, &pkt, ost->st->codec, ost->bitstream_filters); | |
4a4ce2e7 AK |
1514 | } |
1515 | } | |
1516 | } | |
1517 | ||
6291d7e4 AK |
1518 | /* pkt = NULL means EOF (needed to flush decoder buffers) */ |
1519 | static int output_packet(InputStream *ist, int ist_index, | |
4288e031 | 1520 | OutputStream *ost_table, int nb_ostreams, |
6291d7e4 AK |
1521 | const AVPacket *pkt) |
1522 | { | |
1523 | AVFormatContext *os; | |
1524 | OutputStream *ost; | |
1525 | int ret, i; | |
1526 | int got_output; | |
1527 | AVFrame picture; | |
1528 | void *buffer_to_free = NULL; | |
1529 | static unsigned int samples_size= 0; | |
1530 | AVSubtitle subtitle, *subtitle_to_free; | |
1531 | int64_t pkt_pts = AV_NOPTS_VALUE; | |
1532 | #if CONFIG_AVFILTER | |
1533 | int frame_available; | |
1534 | #endif | |
1535 | float quality; | |
1536 | ||
1537 | AVPacket avpkt; | |
1538 | int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt); | |
1539 | ||
1540 | if(ist->next_pts == AV_NOPTS_VALUE) | |
1541 | ist->next_pts= ist->pts; | |
1542 | ||
1543 | if (pkt == NULL) { | |
1544 | /* EOF handling */ | |
1545 | av_init_packet(&avpkt); | |
1546 | avpkt.data = NULL; | |
1547 | avpkt.size = 0; | |
1548 | goto handle_eof; | |
1549 | } else { | |
1550 | avpkt = *pkt; | |
1551 | } | |
1552 | ||
1553 | if(pkt->dts != AV_NOPTS_VALUE) | |
1554 | ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q); | |
1555 | if(pkt->pts != AV_NOPTS_VALUE) | |
1556 | pkt_pts = av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q); | |
1557 | ||
1558 | //while we have more to decode or while the decoder did output something on EOF | |
1559 | while (avpkt.size > 0 || (!pkt && got_output)) { | |
1560 | uint8_t *data_buf, *decoded_data_buf; | |
1561 | int data_size, decoded_data_size; | |
1562 | handle_eof: | |
1563 | ist->pts= ist->next_pts; | |
1564 | ||
1565 | if(avpkt.size && avpkt.size != pkt->size && | |
1566 | ((!ist->showed_multi_packet_warning && verbose>0) || verbose>1)){ | |
1567 | fprintf(stderr, "Multiple frames in a packet from stream %d\n", pkt->stream_index); | |
1568 | ist->showed_multi_packet_warning=1; | |
1569 | } | |
1570 | ||
1571 | /* decode the packet if needed */ | |
1572 | decoded_data_buf = NULL; /* fail safe */ | |
1573 | decoded_data_size= 0; | |
1574 | data_buf = avpkt.data; | |
1575 | data_size = avpkt.size; | |
1576 | subtitle_to_free = NULL; | |
1577 | if (ist->decoding_needed) { | |
1578 | switch(ist->st->codec->codec_type) { | |
1579 | case AVMEDIA_TYPE_AUDIO:{ | |
1580 | if(pkt && samples_size < FFMAX(pkt->size*sizeof(*samples), AVCODEC_MAX_AUDIO_FRAME_SIZE)) { | |
1581 | samples_size = FFMAX(pkt->size*sizeof(*samples), AVCODEC_MAX_AUDIO_FRAME_SIZE); | |
1582 | av_free(samples); | |
1583 | samples= av_malloc(samples_size); | |
1584 | } | |
1585 | decoded_data_size= samples_size; | |
1586 | /* XXX: could avoid copy if PCM 16 bits with same | |
1587 | endianness as CPU */ | |
1588 | ret = avcodec_decode_audio3(ist->st->codec, samples, &decoded_data_size, | |
1589 | &avpkt); | |
1590 | if (ret < 0) | |
1591 | return ret; | |
1592 | avpkt.data += ret; | |
1593 | avpkt.size -= ret; | |
1594 | data_size = ret; | |
1595 | got_output = decoded_data_size > 0; | |
1596 | /* Some bug in mpeg audio decoder gives */ | |
1597 | /* decoded_data_size < 0, it seems they are overflows */ | |
1598 | if (!got_output) { | |
1599 | /* no audio frame */ | |
1600 | continue; | |
1601 | } | |
1602 | decoded_data_buf = (uint8_t *)samples; | |
1603 | ist->next_pts += ((int64_t)AV_TIME_BASE/bps * decoded_data_size) / | |
1604 | (ist->st->codec->sample_rate * ist->st->codec->channels); | |
1605 | break;} | |
1606 | case AVMEDIA_TYPE_VIDEO: | |
1607 | decoded_data_size = (ist->st->codec->width * ist->st->codec->height * 3) / 2; | |
1608 | /* XXX: allocate picture correctly */ | |
1609 | avcodec_get_frame_defaults(&picture); | |
1610 | avpkt.pts = pkt_pts; | |
1611 | avpkt.dts = ist->pts; | |
1612 | pkt_pts = AV_NOPTS_VALUE; | |
1613 | ||
1614 | ret = avcodec_decode_video2(ist->st->codec, | |
1615 | &picture, &got_output, &avpkt); | |
f4ad238c | 1616 | quality = same_quant ? picture.quality : 0; |
6291d7e4 AK |
1617 | if (ret < 0) |
1618 | return ret; | |
1619 | if (!got_output) { | |
1620 | /* no picture yet */ | |
1621 | goto discard_packet; | |
1622 | } | |
1623 | ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, picture.pkt_pts, picture.pkt_dts); | |
1624 | if (ist->st->codec->time_base.num != 0) { | |
1625 | int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame; | |
1626 | ist->next_pts += ((int64_t)AV_TIME_BASE * | |
1627 | ist->st->codec->time_base.num * ticks) / | |
1628 | ist->st->codec->time_base.den; | |
1629 | } | |
1630 | avpkt.size = 0; | |
1631 | buffer_to_free = NULL; | |
1632 | pre_process_video_frame(ist, (AVPicture *)&picture, &buffer_to_free); | |
1633 | break; | |
1634 | case AVMEDIA_TYPE_SUBTITLE: | |
1635 | ret = avcodec_decode_subtitle2(ist->st->codec, | |
1636 | &subtitle, &got_output, &avpkt); | |
1637 | if (ret < 0) | |
1638 | return ret; | |
1639 | if (!got_output) { | |
1640 | goto discard_packet; | |
1641 | } | |
1642 | subtitle_to_free = &subtitle; | |
1643 | avpkt.size = 0; | |
1644 | break; | |
1645 | default: | |
1646 | return -1; | |
1647 | } | |
1648 | } else { | |
1649 | switch(ist->st->codec->codec_type) { | |
1650 | case AVMEDIA_TYPE_AUDIO: | |
1651 | ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) / | |
1652 | ist->st->codec->sample_rate; | |
1653 | break; | |
1654 | case AVMEDIA_TYPE_VIDEO: | |
1655 | if (ist->st->codec->time_base.num != 0) { | |
1656 | int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame; | |
1657 | ist->next_pts += ((int64_t)AV_TIME_BASE * | |
1658 | ist->st->codec->time_base.num * ticks) / | |
1659 | ist->st->codec->time_base.den; | |
1660 | } | |
1661 | break; | |
1662 | } | |
6291d7e4 AK |
1663 | avpkt.size = 0; |
1664 | } | |
1665 | ||
6291d7e4 AK |
1666 | // preprocess audio (volume) |
1667 | if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { | |
1668 | if (audio_volume != 256) { | |
1669 | short *volp; | |
1670 | volp = samples; | |
1671 | for(i=0;i<(decoded_data_size / sizeof(short));i++) { | |
1672 | int v = ((*volp) * audio_volume + 128) >> 8; | |
6c7ee2f7 | 1673 | *volp++ = av_clip_int16(v); |
6291d7e4 AK |
1674 | } |
1675 | } | |
1676 | } | |
1677 | ||
1678 | /* frame rate emulation */ | |
f4805328 | 1679 | if (input_files[ist->file_index].rate_emu) { |
6291d7e4 AK |
1680 | int64_t pts = av_rescale(ist->pts, 1000000, AV_TIME_BASE); |
1681 | int64_t now = av_gettime() - ist->start; | |
1682 | if (pts > now) | |
1683 | usleep(pts - now); | |
1684 | } | |
1685 | /* if output time reached then transcode raw format, | |
1686 | encode packets and output them */ | |
ea065176 | 1687 | for (i = 0; i < nb_ostreams; i++) { |
45f86128 AK |
1688 | OutputFile *of = &output_files[ost_table[i].file_index]; |
1689 | int frame_size; | |
6291d7e4 | 1690 | |
45f86128 AK |
1691 | ost = &ost_table[i]; |
1692 | if (ost->source_index != ist_index) | |
1693 | continue; | |
f1176d41 | 1694 | |
45f86128 AK |
1695 | if (of->start_time && ist->pts < of->start_time) |
1696 | continue; | |
ea065176 | 1697 | |
45f86128 AK |
1698 | if (of->recording_time != INT64_MAX && |
1699 | av_compare_ts(ist->pts, AV_TIME_BASE_Q, of->recording_time + of->start_time, | |
1700 | (AVRational){1, 1000000}) >= 0) { | |
1701 | ost->is_past_recording_time = 1; | |
1702 | continue; | |
1703 | } | |
ef44a607 | 1704 | |
6291d7e4 | 1705 | #if CONFIG_AVFILTER |
45f86128 AK |
1706 | if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && |
1707 | ost->input_video_filter) { | |
1708 | AVRational sar; | |
1709 | if (ist->st->sample_aspect_ratio.num) | |
1710 | sar = ist->st->sample_aspect_ratio; | |
1711 | else | |
1712 | sar = ist->st->codec->sample_aspect_ratio; | |
1713 | av_vsrc_buffer_add_frame(ost->input_video_filter, &picture, ist->pts, sar); | |
1714 | } | |
1715 | frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO || | |
1716 | !ost->output_video_filter || avfilter_poll_frame(ost->output_video_filter->inputs[0]); | |
1717 | while (frame_available) { | |
1718 | AVRational ist_pts_tb; | |
1719 | if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ost->output_video_filter) | |
1720 | get_filtered_video_frame(ost->output_video_filter, &picture, &ost->picref, &ist_pts_tb); | |
1721 | if (ost->picref) | |
1722 | ist->pts = av_rescale_q(ost->picref->pts, ist_pts_tb, AV_TIME_BASE_Q); | |
6291d7e4 | 1723 | #endif |
45f86128 AK |
1724 | os = output_files[ost->file_index].ctx; |
1725 | ||
1726 | /* set the input output pts pairs */ | |
1727 | //ost->sync_ipts = (double)(ist->pts + input_files[ist->file_index].ts_offset - start_time)/ AV_TIME_BASE; | |
1728 | ||
1729 | if (ost->encoding_needed) { | |
1730 | av_assert0(ist->decoding_needed); | |
1731 | switch(ost->st->codec->codec_type) { | |
1732 | case AVMEDIA_TYPE_AUDIO: | |
1733 | do_audio_out(os, ost, ist, decoded_data_buf, decoded_data_size); | |
1734 | break; | |
1735 | case AVMEDIA_TYPE_VIDEO: | |
6291d7e4 | 1736 | #if CONFIG_AVFILTER |
45f86128 AK |
1737 | if (ost->picref->video && !ost->frame_aspect_ratio) |
1738 | ost->st->codec->sample_aspect_ratio = ost->picref->video->pixel_aspect; | |
6291d7e4 | 1739 | #endif |
45f86128 AK |
1740 | do_video_out(os, ost, ist, &picture, &frame_size, |
1741 | same_quant ? quality : ost->st->codec->global_quality); | |
1742 | if (vstats_filename && frame_size) | |
1743 | do_video_stats(os, ost, frame_size); | |
1744 | break; | |
1745 | case AVMEDIA_TYPE_SUBTITLE: | |
1746 | do_subtitle_out(os, ost, ist, &subtitle, | |
1747 | pkt->pts); | |
1748 | break; | |
1749 | default: | |
1750 | abort(); | |
1751 | } | |
1752 | } else { | |
1753 | AVFrame avframe; //FIXME/XXX remove this | |
1754 | AVPacket opkt; | |
1755 | int64_t ost_tb_start_time= av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base); | |
6291d7e4 | 1756 | |
45f86128 | 1757 | av_init_packet(&opkt); |
6291d7e4 | 1758 | |
45f86128 | 1759 | if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) && !copy_initial_nonkeyframes) |
6291d7e4 | 1760 | #if !CONFIG_AVFILTER |
45f86128 | 1761 | continue; |
6291d7e4 | 1762 | #else |
45f86128 | 1763 | goto cont; |
6291d7e4 AK |
1764 | #endif |
1765 | ||
45f86128 AK |
1766 | /* no reencoding needed : output the packet directly */ |
1767 | /* force the input stream PTS */ | |
1768 | ||
1769 | avcodec_get_frame_defaults(&avframe); | |
1770 | ost->st->codec->coded_frame= &avframe; | |
1771 | avframe.key_frame = pkt->flags & AV_PKT_FLAG_KEY; | |
1772 | ||
1773 | if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) | |
1774 | audio_size += data_size; | |
1775 | else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { | |
1776 | video_size += data_size; | |
1777 | ost->sync_opts++; | |
6291d7e4 | 1778 | } |
45f86128 AK |
1779 | |
1780 | opkt.stream_index= ost->index; | |
1781 | if(pkt->pts != AV_NOPTS_VALUE) | |
1782 | opkt.pts= av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time; | |
1783 | else | |
1784 | opkt.pts= AV_NOPTS_VALUE; | |
1785 | ||
1786 | if (pkt->dts == AV_NOPTS_VALUE) | |
1787 | opkt.dts = av_rescale_q(ist->pts, AV_TIME_BASE_Q, ost->st->time_base); | |
1788 | else | |
1789 | opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base); | |
1790 | opkt.dts -= ost_tb_start_time; | |
1791 | ||
1792 | opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base); | |
1793 | opkt.flags= pkt->flags; | |
1794 | ||
1795 | //FIXME remove the following 2 lines they shall be replaced by the bitstream filters | |
1796 | if( ost->st->codec->codec_id != CODEC_ID_H264 | |
1797 | && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO | |
1798 | && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO | |
1799 | ) { | |
1800 | if(av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, data_buf, data_size, pkt->flags & AV_PKT_FLAG_KEY)) | |
1801 | opkt.destruct= av_destruct_packet; | |
1802 | } else { | |
1803 | opkt.data = data_buf; | |
1804 | opkt.size = data_size; | |
1805 | } | |
1806 | ||
1807 | write_frame(os, &opkt, ost->st->codec, ost->bitstream_filters); | |
1808 | ost->st->codec->frame_number++; | |
1809 | ost->frame_number++; | |
1810 | av_free_packet(&opkt); | |
6291d7e4 | 1811 | } |
45f86128 AK |
1812 | #if CONFIG_AVFILTER |
1813 | cont: | |
1814 | frame_available = (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) && | |
1815 | ost->output_video_filter && avfilter_poll_frame(ost->output_video_filter->inputs[0]); | |
1816 | if (ost->picref) | |
1817 | avfilter_unref_buffer(ost->picref); | |
1818 | } | |
6291d7e4 | 1819 | #endif |
6291d7e4 AK |
1820 | } |
1821 | ||
1822 | av_free(buffer_to_free); | |
1823 | /* XXX: allocate the subtitles in the codec ? */ | |
1824 | if (subtitle_to_free) { | |
1825 | avsubtitle_free(subtitle_to_free); | |
1826 | subtitle_to_free = NULL; | |
1827 | } | |
1828 | } | |
1829 | discard_packet: | |
6291d7e4 AK |
1830 | |
1831 | return 0; | |
1832 | } | |
1833 | ||
af70aa45 | 1834 | static void print_sdp(OutputFile *output_files, int n) |
6291d7e4 AK |
1835 | { |
1836 | char sdp[2048]; | |
af70aa45 AK |
1837 | int i; |
1838 | AVFormatContext **avc = av_malloc(sizeof(*avc)*n); | |
1839 | ||
1840 | if (!avc) | |
1841 | exit_program(1); | |
1842 | for (i = 0; i < n; i++) | |
1843 | avc[i] = output_files[i].ctx; | |
6291d7e4 AK |
1844 | |
1845 | av_sdp_create(avc, n, sdp, sizeof(sdp)); | |
1846 | printf("SDP:\n%s\n", sdp); | |
1847 | fflush(stdout); | |
af70aa45 | 1848 | av_freep(&avc); |
6291d7e4 AK |
1849 | } |
1850 | ||
630902a1 AK |
1851 | static int init_input_stream(int ist_index, OutputStream *output_streams, int nb_output_streams, |
1852 | char *error, int error_len) | |
1853 | { | |
1854 | int i; | |
1855 | InputStream *ist = &input_streams[ist_index]; | |
1856 | if (ist->decoding_needed) { | |
1857 | AVCodec *codec = ist->dec; | |
630902a1 | 1858 | if (!codec) { |
62486948 | 1859 | snprintf(error, error_len, "Decoder (codec id %d) not found for input stream #%d.%d", |
630902a1 AK |
1860 | ist->st->codec->codec_id, ist->file_index, ist->st->index); |
1861 | return AVERROR(EINVAL); | |
1862 | } | |
1863 | ||
1864 | /* update requested sample format for the decoder based on the | |
1865 | corresponding encoder sample format */ | |
1866 | for (i = 0; i < nb_output_streams; i++) { | |
1867 | OutputStream *ost = &output_streams[i]; | |
1868 | if (ost->source_index == ist_index) { | |
1869 | update_sample_fmt(ist->st->codec, codec, ost->st->codec); | |
1870 | break; | |
1871 | } | |
1872 | } | |
1873 | ||
1874 | if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) { | |
62486948 | 1875 | snprintf(error, error_len, "Error while opening decoder for input stream #%d.%d", |
630902a1 AK |
1876 | ist->file_index, ist->st->index); |
1877 | return AVERROR(EINVAL); | |
1878 | } | |
1879 | assert_codec_experimental(ist->st->codec, 0); | |
1880 | assert_avoptions(ist->opts); | |
1881 | } | |
1882 | ||
1883 | ist->pts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames*AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0; | |
1884 | ist->next_pts = AV_NOPTS_VALUE; | |
1885 | init_pts_correction(&ist->pts_ctx); | |
1886 | ist->is_start = 1; | |
1887 | ||
1888 | return 0; | |
1889 | } | |
1890 | ||
eaf2d37a AC |
1891 | static int transcode_init(OutputFile *output_files, |
1892 | int nb_output_files, | |
1893 | InputFile *input_files, | |
1894 | int nb_input_files) | |
6291d7e4 | 1895 | { |
2c474ddb | 1896 | int ret = 0, i, j, k; |
eaf2d37a | 1897 | AVFormatContext *os; |
6291d7e4 | 1898 | AVCodecContext *codec, *icodec; |
4288e031 | 1899 | OutputStream *ost; |
6291d7e4 AK |
1900 | InputStream *ist; |
1901 | char error[1024]; | |
1902 | int want_sdp = 1; | |
b0c9e8e0 | 1903 | |
f4805328 AK |
1904 | /* init framerate emulation */ |
1905 | for (i = 0; i < nb_input_files; i++) { | |
1906 | InputFile *ifile = &input_files[i]; | |
1907 | if (ifile->rate_emu) | |
1908 | for (j = 0; j < ifile->nb_streams; j++) | |
1909 | input_streams[j + ifile->ist_index].start = av_gettime(); | |
1910 | } | |
6291d7e4 AK |
1911 | |
1912 | /* output stream init */ | |
6291d7e4 | 1913 | for(i=0;i<nb_output_files;i++) { |
af70aa45 | 1914 | os = output_files[i].ctx; |
6291d7e4 | 1915 | if (!os->nb_streams && !(os->oformat->flags & AVFMT_NOSTREAMS)) { |
af70aa45 | 1916 | av_dump_format(os, i, os->filename, 1); |
6291d7e4 | 1917 | fprintf(stderr, "Output file #%d does not contain any stream\n", i); |
eaf2d37a | 1918 | return AVERROR(EINVAL); |
6291d7e4 | 1919 | } |
6291d7e4 AK |
1920 | } |
1921 | ||
1922 | /* for each output stream, we compute the right encoding parameters */ | |
4288e031 AK |
1923 | for (i = 0; i < nb_output_streams; i++) { |
1924 | ost = &output_streams[i]; | |
af70aa45 | 1925 | os = output_files[ost->file_index].ctx; |
6291d7e4 AK |
1926 | ist = &input_streams[ost->source_index]; |
1927 | ||
1928 | codec = ost->st->codec; | |
1929 | icodec = ist->st->codec; | |
1930 | ||
6291d7e4 AK |
1931 | ost->st->disposition = ist->st->disposition; |
1932 | codec->bits_per_raw_sample= icodec->bits_per_raw_sample; | |
1933 | codec->chroma_sample_location = icodec->chroma_sample_location; | |
1934 | ||
1935 | if (ost->st->stream_copy) { | |
1936 | uint64_t extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE; | |
1937 | ||
e6d2b737 | 1938 | if (extra_size > INT_MAX) { |
eaf2d37a | 1939 | return AVERROR(EINVAL); |
e6d2b737 | 1940 | } |
6291d7e4 AK |
1941 | |
1942 | /* if stream_copy is selected, no need to decode or encode */ | |
1943 | codec->codec_id = icodec->codec_id; | |
1944 | codec->codec_type = icodec->codec_type; | |
1945 | ||
1946 | if(!codec->codec_tag){ | |
1947 | if( !os->oformat->codec_tag | |
1948 | || av_codec_get_id (os->oformat->codec_tag, icodec->codec_tag) == codec->codec_id | |
1949 | || av_codec_get_tag(os->oformat->codec_tag, icodec->codec_id) <= 0) | |
1950 | codec->codec_tag = icodec->codec_tag; | |
1951 | } | |
1952 | ||
1953 | codec->bit_rate = icodec->bit_rate; | |
1954 | codec->rc_max_rate = icodec->rc_max_rate; | |
1955 | codec->rc_buffer_size = icodec->rc_buffer_size; | |
1956 | codec->extradata= av_mallocz(extra_size); | |
e6d2b737 | 1957 | if (!codec->extradata) { |
eaf2d37a | 1958 | return AVERROR(ENOMEM); |
e6d2b737 | 1959 | } |
6291d7e4 AK |
1960 | memcpy(codec->extradata, icodec->extradata, icodec->extradata_size); |
1961 | codec->extradata_size= icodec->extradata_size; | |
1962 | if(!copy_tb && av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/500){ | |
1963 | codec->time_base = icodec->time_base; | |
1964 | codec->time_base.num *= icodec->ticks_per_frame; | |
1965 | av_reduce(&codec->time_base.num, &codec->time_base.den, | |
1966 | codec->time_base.num, codec->time_base.den, INT_MAX); | |
1967 | }else | |
1968 | codec->time_base = ist->st->time_base; | |
1969 | switch(codec->codec_type) { | |
1970 | case AVMEDIA_TYPE_AUDIO: | |
1971 | if(audio_volume != 256) { | |
1972 | fprintf(stderr,"-acodec copy and -vol are incompatible (frames are not decoded)\n"); | |
1973 | exit_program(1); | |
1974 | } | |
1975 | codec->channel_layout = icodec->channel_layout; | |
1976 | codec->sample_rate = icodec->sample_rate; | |
1977 | codec->channels = icodec->channels; | |
1978 | codec->frame_size = icodec->frame_size; | |
1979 | codec->audio_service_type = icodec->audio_service_type; | |
1980 | codec->block_align= icodec->block_align; | |
1981 | if(codec->block_align == 1 && codec->codec_id == CODEC_ID_MP3) | |
1982 | codec->block_align= 0; | |
1983 | if(codec->codec_id == CODEC_ID_AC3) | |
1984 | codec->block_align= 0; | |
1985 | break; | |
1986 | case AVMEDIA_TYPE_VIDEO: | |
1987 | codec->pix_fmt = icodec->pix_fmt; | |
1988 | codec->width = icodec->width; | |
1989 | codec->height = icodec->height; | |
1990 | codec->has_b_frames = icodec->has_b_frames; | |
1991 | if (!codec->sample_aspect_ratio.num) { | |
1992 | codec->sample_aspect_ratio = | |
1993 | ost->st->sample_aspect_ratio = | |
1994 | ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio : | |
1995 | ist->st->codec->sample_aspect_ratio.num ? | |
1996 | ist->st->codec->sample_aspect_ratio : (AVRational){0, 1}; | |
1997 | } | |
1998 | break; | |
1999 | case AVMEDIA_TYPE_SUBTITLE: | |
2000 | codec->width = icodec->width; | |
2001 | codec->height = icodec->height; | |
2002 | break; | |
2003 | case AVMEDIA_TYPE_DATA: | |
2004 | break; | |
2005 | default: | |
2006 | abort(); | |
2007 | } | |
2008 | } else { | |
2009 | if (!ost->enc) | |
2010 | ost->enc = avcodec_find_encoder(ost->st->codec->codec_id); | |
2011 | switch(codec->codec_type) { | |
2012 | case AVMEDIA_TYPE_AUDIO: | |
2013 | ost->fifo= av_fifo_alloc(1024); | |
e6d2b737 | 2014 | if (!ost->fifo) { |
eaf2d37a | 2015 | return AVERROR(ENOMEM); |
e6d2b737 | 2016 | } |
6291d7e4 AK |
2017 | ost->reformat_pair = MAKE_SFMT_PAIR(AV_SAMPLE_FMT_NONE,AV_SAMPLE_FMT_NONE); |
2018 | if (!codec->sample_rate) { | |
2019 | codec->sample_rate = icodec->sample_rate; | |
2020 | if (icodec->lowres) | |
2021 | codec->sample_rate >>= icodec->lowres; | |
2022 | } | |
2023 | choose_sample_rate(ost->st, ost->enc); | |
2024 | codec->time_base = (AVRational){1, codec->sample_rate}; | |
2025 | if (codec->sample_fmt == AV_SAMPLE_FMT_NONE) | |
2026 | codec->sample_fmt = icodec->sample_fmt; | |
2027 | choose_sample_fmt(ost->st, ost->enc); | |
2028 | if (!codec->channels) | |
2029 | codec->channels = icodec->channels; | |
2030 | codec->channel_layout = icodec->channel_layout; | |
2031 | if (av_get_channel_layout_nb_channels(codec->channel_layout) != codec->channels) | |
2032 | codec->channel_layout = 0; | |
2033 | ost->audio_resample = codec->sample_rate != icodec->sample_rate || audio_sync_method > 1; | |
2034 | icodec->request_channels = codec->channels; | |
2035 | ist->decoding_needed = 1; | |
2036 | ost->encoding_needed = 1; | |
2037 | ost->resample_sample_fmt = icodec->sample_fmt; | |
2038 | ost->resample_sample_rate = icodec->sample_rate; | |
2039 | ost->resample_channels = icodec->channels; | |
2040 | break; | |
2041 | case AVMEDIA_TYPE_VIDEO: | |
2042 | if (codec->pix_fmt == PIX_FMT_NONE) | |
2043 | codec->pix_fmt = icodec->pix_fmt; | |
2044 | choose_pixel_fmt(ost->st, ost->enc); | |
2045 | ||
2046 | if (ost->st->codec->pix_fmt == PIX_FMT_NONE) { | |
2047 | fprintf(stderr, "Video pixel format is unknown, stream cannot be encoded\n"); | |
2048 | exit_program(1); | |
2049 | } | |
2050 | ||
2051 | if (!codec->width || !codec->height) { | |
2052 | codec->width = icodec->width; | |
2053 | codec->height = icodec->height; | |
2054 | } | |
2055 | ||
2056 | ost->video_resample = codec->width != icodec->width || | |
2057 | codec->height != icodec->height || | |
2058 | codec->pix_fmt != icodec->pix_fmt; | |
2059 | if (ost->video_resample) { | |
2060 | #if !CONFIG_AVFILTER | |
2061 | avcodec_get_frame_defaults(&ost->pict_tmp); | |
2062 | if(avpicture_alloc((AVPicture*)&ost->pict_tmp, codec->pix_fmt, | |
2063 | codec->width, codec->height)) { | |
2064 | fprintf(stderr, "Cannot allocate temp picture, check pix fmt\n"); | |
2065 | exit_program(1); | |
2066 | } | |
2067 | ost->img_resample_ctx = sws_getContext( | |
2068 | icodec->width, | |
2069 | icodec->height, | |
2070 | icodec->pix_fmt, | |
2071 | codec->width, | |
2072 | codec->height, | |
2073 | codec->pix_fmt, | |
2074 | ost->sws_flags, NULL, NULL, NULL); | |
2075 | if (ost->img_resample_ctx == NULL) { | |
2076 | fprintf(stderr, "Cannot get resampling context\n"); | |
2077 | exit_program(1); | |
2078 | } | |
2079 | #endif | |
2080 | codec->bits_per_raw_sample= 0; | |
2081 | } | |
2082 | ||
2083 | ost->resample_height = icodec->height; | |
2084 | ost->resample_width = icodec->width; | |
2085 | ost->resample_pix_fmt= icodec->pix_fmt; | |
2086 | ost->encoding_needed = 1; | |
2087 | ist->decoding_needed = 1; | |
2088 | ||
2089 | if (!ost->frame_rate.num) | |
2090 | ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25,1}; | |
bef737a7 | 2091 | if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) { |
6291d7e4 AK |
2092 | int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates); |
2093 | ost->frame_rate = ost->enc->supported_framerates[idx]; | |
2094 | } | |
2095 | codec->time_base = (AVRational){ost->frame_rate.den, ost->frame_rate.num}; | |
2096 | ||
2097 | #if CONFIG_AVFILTER | |
2098 | if (configure_video_filters(ist, ost)) { | |
2099 | fprintf(stderr, "Error opening filters!\n"); | |
2100 | exit(1); | |
2101 | } | |
2102 | #endif | |
2103 | break; | |
2104 | case AVMEDIA_TYPE_SUBTITLE: | |
2105 | ost->encoding_needed = 1; | |
2106 | ist->decoding_needed = 1; | |
2107 | break; | |
2108 | default: | |
2109 | abort(); | |
2110 | break; | |
2111 | } | |
2112 | /* two pass mode */ | |
2113 | if (ost->encoding_needed && | |
2114 | (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) { | |
2115 | char logfilename[1024]; | |
2116 | FILE *f; | |
2117 | ||
2118 | snprintf(logfilename, sizeof(logfilename), "%s-%d.log", | |
2119 | pass_logfilename_prefix ? pass_logfilename_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX, | |
2120 | i); | |
2121 | if (codec->flags & CODEC_FLAG_PASS1) { | |
2122 | f = fopen(logfilename, "wb"); | |
2123 | if (!f) { | |
2124 | fprintf(stderr, "Cannot write log file '%s' for pass-1 encoding: %s\n", logfilename, strerror(errno)); | |
2125 | exit_program(1); | |
2126 | } | |
2127 | ost->logfile = f; | |
2128 | } else { | |
2129 | char *logbuffer; | |
2130 | size_t logbuffer_size; | |
2131 | if (read_file(logfilename, &logbuffer, &logbuffer_size) < 0) { | |
2132 | fprintf(stderr, "Error reading log file '%s' for pass-2 encoding\n", logfilename); | |
2133 | exit_program(1); | |
2134 | } | |
2135 | codec->stats_in = logbuffer; | |
2136 | } | |
2137 | } | |
2138 | } | |
2139 | if(codec->codec_type == AVMEDIA_TYPE_VIDEO){ | |
2140 | int size= codec->width * codec->height; | |
2141 | bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 200); | |
2142 | } | |
2143 | } | |
2144 | ||
2145 | if (!bit_buffer) | |
2146 | bit_buffer = av_malloc(bit_buffer_size); | |
2147 | if (!bit_buffer) { | |
2148 | fprintf(stderr, "Cannot allocate %d bytes output buffer\n", | |
2149 | bit_buffer_size); | |
eaf2d37a | 2150 | return AVERROR(ENOMEM); |
6291d7e4 AK |
2151 | } |
2152 | ||
2153 | /* open each encoder */ | |
4288e031 AK |
2154 | for (i = 0; i < nb_output_streams; i++) { |
2155 | ost = &output_streams[i]; | |
6291d7e4 AK |
2156 | if (ost->encoding_needed) { |
2157 | AVCodec *codec = ost->enc; | |
2158 | AVCodecContext *dec = input_streams[ost->source_index].st->codec; | |
2159 | if (!codec) { | |
2160 | snprintf(error, sizeof(error), "Encoder (codec id %d) not found for output stream #%d.%d", | |
2161 | ost->st->codec->codec_id, ost->file_index, ost->index); | |
2162 | ret = AVERROR(EINVAL); | |
2163 | goto dump_format; | |
2164 | } | |
2165 | if (dec->subtitle_header) { | |
2166 | ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size); | |
2167 | if (!ost->st->codec->subtitle_header) { | |
2168 | ret = AVERROR(ENOMEM); | |
2169 | goto dump_format; | |
2170 | } | |
2171 | memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size); | |
2172 | ost->st->codec->subtitle_header_size = dec->subtitle_header_size; | |
2173 | } | |
2174 | if (avcodec_open2(ost->st->codec, codec, &ost->opts) < 0) { | |
2175 | snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height", | |
2176 | ost->file_index, ost->index); | |
2177 | ret = AVERROR(EINVAL); | |
2178 | goto dump_format; | |
2179 | } | |
2180 | assert_codec_experimental(ost->st->codec, 1); | |
2181 | assert_avoptions(ost->opts); | |
2182 | if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000) | |
2183 | av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low." | |
2184 | "It takes bits/s as argument, not kbits/s\n"); | |
2185 | extra_size += ost->st->codec->extradata_size; | |
2186 | } | |
2187 | } | |
2188 | ||
630902a1 AK |
2189 | /* init input streams */ |
2190 | for (i = 0; i < nb_input_streams; i++) | |
62486948 | 2191 | if ((ret = init_input_stream(i, output_streams, nb_output_streams, error, sizeof(error))) < 0) |
630902a1 | 2192 | goto dump_format; |
6291d7e4 | 2193 | |
2c474ddb AK |
2194 | /* discard unused programs */ |
2195 | for (i = 0; i < nb_input_files; i++) { | |
2196 | InputFile *ifile = &input_files[i]; | |
2197 | for (j = 0; j < ifile->ctx->nb_programs; j++) { | |
2198 | AVProgram *p = ifile->ctx->programs[j]; | |
2199 | int discard = AVDISCARD_ALL; | |
2200 | ||
2201 | for (k = 0; k < p->nb_stream_indexes; k++) | |
2202 | if (!input_streams[ifile->ist_index + p->stream_index[k]].discard) { | |
2203 | discard = AVDISCARD_DEFAULT; | |
2204 | break; | |
2205 | } | |
2206 | p->discard = discard; | |
2207 | } | |
2208 | } | |
2209 | ||
6291d7e4 | 2210 | /* open files and write file headers */ |
af70aa45 AK |
2211 | for (i = 0; i < nb_output_files; i++) { |
2212 | os = output_files[i].ctx; | |
2213 | if (avformat_write_header(os, &output_files[i].opts) < 0) { | |
6291d7e4 AK |
2214 | snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?)", i); |
2215 | ret = AVERROR(EINVAL); | |
2216 | goto dump_format; | |
2217 | } | |
af70aa45 AK |
2218 | assert_avoptions(output_files[i].opts); |
2219 | if (strcmp(os->oformat->name, "rtp")) { | |
6291d7e4 AK |
2220 | want_sdp = 0; |
2221 | } | |
2222 | } | |
2223 | ||
2224 | dump_format: | |
2225 | /* dump the file output parameters - cannot be done before in case | |
2226 | of stream copy */ | |
2227 | for(i=0;i<nb_output_files;i++) { | |
af70aa45 | 2228 | av_dump_format(output_files[i].ctx, i, output_files[i].ctx->filename, 1); |
6291d7e4 AK |
2229 | } |
2230 | ||
2231 | /* dump the stream mapping */ | |
2232 | if (verbose >= 0) { | |
2233 | fprintf(stderr, "Stream mapping:\n"); | |
4288e031 AK |
2234 | for (i = 0; i < nb_output_streams;i ++) { |
2235 | ost = &output_streams[i]; | |
6291d7e4 AK |
2236 | fprintf(stderr, " Stream #%d.%d -> #%d.%d", |
2237 | input_streams[ost->source_index].file_index, | |
2238 | input_streams[ost->source_index].st->index, | |
2239 | ost->file_index, | |
2240 | ost->index); | |
2241 | if (ost->sync_ist != &input_streams[ost->source_index]) | |
2242 | fprintf(stderr, " [sync #%d.%d]", | |
2243 | ost->sync_ist->file_index, | |
2244 | ost->sync_ist->st->index); | |
ad42126a AK |
2245 | if (ost->st->stream_copy) |
2246 | fprintf(stderr, " (copy)"); | |
bc8c5051 NG |
2247 | else |
2248 | fprintf(stderr, " (%s -> %s)", input_streams[ost->source_index].dec ? | |
2249 | input_streams[ost->source_index].dec->name : "?", | |
2250 | ost->enc ? ost->enc->name : "?"); | |
6291d7e4 AK |
2251 | fprintf(stderr, "\n"); |
2252 | } | |
2253 | } | |
2254 | ||
2255 | if (ret) { | |
2256 | fprintf(stderr, "%s\n", error); | |
eaf2d37a | 2257 | return ret; |
6291d7e4 AK |
2258 | } |
2259 | ||
2260 | if (want_sdp) { | |
2261 | print_sdp(output_files, nb_output_files); | |
2262 | } | |
2263 | ||
eaf2d37a AC |
2264 | return 0; |
2265 | } | |
2266 | ||
2267 | /* | |
2268 | * The following code is the main loop of the file converter | |
2269 | */ | |
2270 | static int transcode(OutputFile *output_files, | |
2271 | int nb_output_files, | |
2272 | InputFile *input_files, | |
2273 | int nb_input_files) | |
2274 | { | |
2275 | int ret, i; | |
2276 | AVFormatContext *is, *os; | |
2277 | OutputStream *ost; | |
2278 | InputStream *ist; | |
2279 | uint8_t *no_packet; | |
2280 | int no_packet_count=0; | |
2281 | int64_t timer_start; | |
2282 | ||
2283 | if (!(no_packet = av_mallocz(nb_input_files))) | |
2284 | exit_program(1); | |
2285 | ||
2286 | ret = transcode_init(output_files, nb_output_files, input_files, nb_input_files); | |
2287 | if (ret < 0) | |
2288 | goto fail; | |
2289 | ||
6291d7e4 AK |
2290 | if (verbose >= 0) |
2291 | fprintf(stderr, "Press ctrl-c to stop encoding\n"); | |
2292 | term_init(); | |
2293 | ||
2294 | timer_start = av_gettime(); | |
2295 | ||
2296 | for(; received_sigterm == 0;) { | |
2297 | int file_index, ist_index; | |
2298 | AVPacket pkt; | |
9be3c124 | 2299 | int64_t ipts_min; |
6291d7e4 AK |
2300 | double opts_min; |
2301 | ||
9be3c124 | 2302 | ipts_min = INT64_MAX; |
6291d7e4 AK |
2303 | opts_min= 1e100; |
2304 | ||
2305 | /* select the stream that we must read now by looking at the | |
2306 | smallest output pts */ | |
2307 | file_index = -1; | |
4288e031 | 2308 | for (i = 0; i < nb_output_streams; i++) { |
f21f294e | 2309 | OutputFile *of; |
9be3c124 AC |
2310 | int64_t ipts; |
2311 | double opts; | |
4288e031 | 2312 | ost = &output_streams[i]; |
f21f294e | 2313 | of = &output_files[ost->file_index]; |
af70aa45 | 2314 | os = output_files[ost->file_index].ctx; |
6291d7e4 | 2315 | ist = &input_streams[ost->source_index]; |
f21f294e AK |
2316 | if (ost->is_past_recording_time || no_packet[ist->file_index] || |
2317 | (os->pb && avio_tell(os->pb) >= of->limit_filesize)) | |
6291d7e4 | 2318 | continue; |
c0931508 | 2319 | opts = ost->st->pts.val * av_q2d(ost->st->time_base); |
9be3c124 | 2320 | ipts = ist->pts; |
6291d7e4 AK |
2321 | if (!input_files[ist->file_index].eof_reached){ |
2322 | if(ipts < ipts_min) { | |
2323 | ipts_min = ipts; | |
2324 | if(input_sync ) file_index = ist->file_index; | |
2325 | } | |
2326 | if(opts < opts_min) { | |
2327 | opts_min = opts; | |
2328 | if(!input_sync) file_index = ist->file_index; | |
2329 | } | |
2330 | } | |
96139b5e AK |
2331 | if (ost->frame_number >= ost->max_frames) { |
2332 | int j; | |
2333 | for (j = of->ost_index; j < of->ctx->nb_streams; j++) | |
2334 | output_streams[j].is_past_recording_time = 1; | |
2335 | continue; | |
6291d7e4 AK |
2336 | } |
2337 | } | |
2338 | /* if none, if is finished */ | |
2339 | if (file_index < 0) { | |
2340 | if(no_packet_count){ | |
2341 | no_packet_count=0; | |
b0c9e8e0 | 2342 | memset(no_packet, 0, nb_input_files); |
6291d7e4 AK |
2343 | usleep(10000); |
2344 | continue; | |
2345 | } | |
2346 | break; | |
2347 | } | |
2348 | ||
6291d7e4 AK |
2349 | /* read a frame from it and output it in the fifo */ |
2350 | is = input_files[file_index].ctx; | |
2351 | ret= av_read_frame(is, &pkt); | |
2352 | if(ret == AVERROR(EAGAIN)){ | |
2353 | no_packet[file_index]=1; | |
2354 | no_packet_count++; | |
2355 | continue; | |
2356 | } | |
2357 | if (ret < 0) { | |
2358 | input_files[file_index].eof_reached = 1; | |
2359 | if (opt_shortest) | |
2360 | break; | |
2361 | else | |
2362 | continue; | |
2363 | } | |
2364 | ||
2365 | no_packet_count=0; | |
b0c9e8e0 | 2366 | memset(no_packet, 0, nb_input_files); |
6291d7e4 AK |
2367 | |
2368 | if (do_pkt_dump) { | |
2369 | av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump, | |
2370 | is->streams[pkt.stream_index]); | |
2371 | } | |
2372 | /* the following test is needed in case new streams appear | |
2373 | dynamically in stream : we ignore them */ | |
ed5b1326 | 2374 | if (pkt.stream_index >= input_files[file_index].nb_streams) |
6291d7e4 AK |
2375 | goto discard_packet; |
2376 | ist_index = input_files[file_index].ist_index + pkt.stream_index; | |
2377 | ist = &input_streams[ist_index]; | |
2378 | if (ist->discard) | |
2379 | goto discard_packet; | |
2380 | ||
2381 | if (pkt.dts != AV_NOPTS_VALUE) | |
2382 | pkt.dts += av_rescale_q(input_files[ist->file_index].ts_offset, AV_TIME_BASE_Q, ist->st->time_base); | |
2383 | if (pkt.pts != AV_NOPTS_VALUE) | |
2384 | pkt.pts += av_rescale_q(input_files[ist->file_index].ts_offset, AV_TIME_BASE_Q, ist->st->time_base); | |
2385 | ||
33f75d72 AK |
2386 | if(pkt.pts != AV_NOPTS_VALUE) |
2387 | pkt.pts *= ist->ts_scale; | |
2388 | if(pkt.dts != AV_NOPTS_VALUE) | |
2389 | pkt.dts *= ist->ts_scale; | |
6291d7e4 AK |
2390 | |
2391 | // fprintf(stderr, "next:%"PRId64" dts:%"PRId64" off:%"PRId64" %d\n", ist->next_pts, pkt.dts, input_files[ist->file_index].ts_offset, ist->st->codec->codec_type); | |
2392 | if (pkt.dts != AV_NOPTS_VALUE && ist->next_pts != AV_NOPTS_VALUE | |
2393 | && (is->iformat->flags & AVFMT_TS_DISCONT)) { | |
2394 | int64_t pkt_dts= av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q); | |
2395 | int64_t delta= pkt_dts - ist->next_pts; | |
2396 | if((FFABS(delta) > 1LL*dts_delta_threshold*AV_TIME_BASE || pkt_dts+1<ist->pts)&& !copy_ts){ | |
2397 | input_files[ist->file_index].ts_offset -= delta; | |
2398 | if (verbose > 2) | |
2399 | fprintf(stderr, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n", | |
2400 | delta, input_files[ist->file_index].ts_offset); | |
2401 | pkt.dts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base); | |
2402 | if(pkt.pts != AV_NOPTS_VALUE) | |
2403 | pkt.pts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base); | |
2404 | } | |
2405 | } | |
2406 | ||
6291d7e4 | 2407 | //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->st->index, pkt.size); |
4288e031 | 2408 | if (output_packet(ist, ist_index, output_streams, nb_output_streams, &pkt) < 0) { |
6291d7e4 AK |
2409 | |
2410 | if (verbose >= 0) | |
2411 | fprintf(stderr, "Error while decoding stream #%d.%d\n", | |
2412 | ist->file_index, ist->st->index); | |
2413 | if (exit_on_error) | |
2414 | exit_program(1); | |
2415 | av_free_packet(&pkt); | |
b9630bcf | 2416 | continue; |
6291d7e4 AK |
2417 | } |
2418 | ||
2419 | discard_packet: | |
2420 | av_free_packet(&pkt); | |
2421 | ||
2422 | /* dump report by using the output first video and audio streams */ | |
c5ad2c2c | 2423 | print_report(output_files, output_streams, nb_output_streams, 0, timer_start); |
6291d7e4 AK |
2424 | } |
2425 | ||
2426 | /* at the end of stream, we must flush the decoder buffers */ | |
2427 | for (i = 0; i < nb_input_streams; i++) { | |
2428 | ist = &input_streams[i]; | |
2429 | if (ist->decoding_needed) { | |
4288e031 | 2430 | output_packet(ist, i, output_streams, nb_output_streams, NULL); |
6291d7e4 AK |
2431 | } |
2432 | } | |
b62b5cb6 | 2433 | flush_encoders(output_streams, nb_output_streams); |
6291d7e4 AK |
2434 | |
2435 | term_exit(); | |
2436 | ||
2437 | /* write the trailer if needed and close file */ | |
2438 | for(i=0;i<nb_output_files;i++) { | |
af70aa45 | 2439 | os = output_files[i].ctx; |
6291d7e4 AK |
2440 | av_write_trailer(os); |
2441 | } | |
2442 | ||
2443 | /* dump report by using the first video and audio streams */ | |
c5ad2c2c | 2444 | print_report(output_files, output_streams, nb_output_streams, 1, timer_start); |
6291d7e4 AK |
2445 | |
2446 | /* close each encoder */ | |
4288e031 AK |
2447 | for (i = 0; i < nb_output_streams; i++) { |
2448 | ost = &output_streams[i]; | |
6291d7e4 AK |
2449 | if (ost->encoding_needed) { |
2450 | av_freep(&ost->st->codec->stats_in); | |
2451 | avcodec_close(ost->st->codec); | |
2452 | } | |
2453 | #if CONFIG_AVFILTER | |
2454 | avfilter_graph_free(&ost->graph); | |
2455 | #endif | |
2456 | } | |
2457 | ||
2458 | /* close each decoder */ | |
2459 | for (i = 0; i < nb_input_streams; i++) { | |
2460 | ist = &input_streams[i]; | |
2461 | if (ist->decoding_needed) { | |
2462 | avcodec_close(ist->st->codec); | |
2463 | } | |
2464 | } | |
2465 | ||
2466 | /* finished ! */ | |
2467 | ret = 0; | |
2468 | ||
2469 | fail: | |
2470 | av_freep(&bit_buffer); | |
b0c9e8e0 | 2471 | av_freep(&no_packet); |
6291d7e4 | 2472 | |
4288e031 AK |
2473 | if (output_streams) { |
2474 | for (i = 0; i < nb_output_streams; i++) { | |
2475 | ost = &output_streams[i]; | |
6291d7e4 AK |
2476 | if (ost) { |
2477 | if (ost->st->stream_copy) | |
2478 | av_freep(&ost->st->codec->extradata); | |
2479 | if (ost->logfile) { | |
2480 | fclose(ost->logfile); | |
2481 | ost->logfile = NULL; | |
2482 | } | |
2483 | av_fifo_free(ost->fifo); /* works even if fifo is not | |
2484 | initialized but set to zero */ | |
2485 | av_freep(&ost->st->codec->subtitle_header); | |
2486 | av_free(ost->pict_tmp.data[0]); | |
2487 | av_free(ost->forced_kf_pts); | |
2488 | if (ost->video_resample) | |
2489 | sws_freeContext(ost->img_resample_ctx); | |
2490 | if (ost->resample) | |
2491 | audio_resample_close(ost->resample); | |
2492 | if (ost->reformat_ctx) | |
2493 | av_audio_convert_free(ost->reformat_ctx); | |
2494 | av_dict_free(&ost->opts); | |
6291d7e4 AK |
2495 | } |
2496 | } | |
6291d7e4 AK |
2497 | } |
2498 | return ret; | |
2499 | } | |
2500 | ||
6291d7e4 AK |
2501 | static int opt_video_rc_override_string(const char *opt, const char *arg) |
2502 | { | |
2503 | video_rc_override_string = arg; | |
2504 | return 0; | |
2505 | } | |
2506 | ||
2507 | static int opt_me_threshold(const char *opt, const char *arg) | |
2508 | { | |
2509 | me_threshold = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX); | |
2510 | return 0; | |
2511 | } | |
2512 | ||
2513 | static int opt_verbose(const char *opt, const char *arg) | |
2514 | { | |
2515 | verbose = parse_number_or_die(opt, arg, OPT_INT64, -10, 10); | |
2516 | return 0; | |
2517 | } | |
2518 | ||
2519 | static int opt_frame_rate(const char *opt, const char *arg) | |
2520 | { | |
2521 | if (av_parse_video_rate(&frame_rate, arg) < 0) { | |
2522 | fprintf(stderr, "Incorrect value for %s: %s\n", opt, arg); | |
2523 | exit_program(1); | |
2524 | } | |
2525 | return 0; | |
2526 | } | |
2527 | ||
6291d7e4 AK |
2528 | static int opt_frame_size(const char *opt, const char *arg) |
2529 | { | |
2530 | if (av_parse_video_size(&frame_width, &frame_height, arg) < 0) { | |
2531 | fprintf(stderr, "Incorrect frame size\n"); | |
2532 | return AVERROR(EINVAL); | |
2533 | } | |
2534 | return 0; | |
2535 | } | |
2536 | ||
6291d7e4 AK |
2537 | static int opt_frame_pix_fmt(const char *opt, const char *arg) |
2538 | { | |
2539 | if (strcmp(arg, "list")) { | |
2540 | frame_pix_fmt = av_get_pix_fmt(arg); | |
2541 | if (frame_pix_fmt == PIX_FMT_NONE) { | |
2542 | fprintf(stderr, "Unknown pixel format requested: %s\n", arg); | |
2543 | return AVERROR(EINVAL); | |
2544 | } | |
2545 | } else { | |
2546 | show_pix_fmts(); | |
2547 | exit_program(0); | |
2548 | } | |
2549 | return 0; | |
2550 | } | |
2551 | ||
2552 | static int opt_frame_aspect_ratio(const char *opt, const char *arg) | |
2553 | { | |
2554 | int x = 0, y = 0; | |
2555 | double ar = 0; | |
2556 | const char *p; | |
2557 | char *end; | |
2558 | ||
2559 | p = strchr(arg, ':'); | |
2560 | if (p) { | |
2561 | x = strtol(arg, &end, 10); | |
2562 | if (end == p) | |
2563 | y = strtol(end+1, &end, 10); | |
2564 | if (x > 0 && y > 0) | |
2565 | ar = (double)x / (double)y; | |
2566 | } else | |
2567 | ar = strtod(arg, NULL); | |
2568 | ||
2569 | if (!ar) { | |
2570 | fprintf(stderr, "Incorrect aspect ratio specification.\n"); | |
2571 | return AVERROR(EINVAL); | |
2572 | } | |
2573 | frame_aspect_ratio = ar; | |
2574 | return 0; | |
2575 | } | |
2576 | ||
6291d7e4 AK |
2577 | static int opt_top_field_first(const char *opt, const char *arg) |
2578 | { | |
2579 | top_field_first = parse_number_or_die(opt, arg, OPT_INT, 0, 1); | |
2580 | return 0; | |
2581 | } | |
2582 | ||
35e6f8c1 | 2583 | static int opt_audio_codec(OptionsContext *o, const char *opt, const char *arg) |
6291d7e4 | 2584 | { |
35e6f8c1 | 2585 | return parse_option(o, "codec:a", arg, options); |
6291d7e4 AK |
2586 | } |
2587 | ||
35e6f8c1 | 2588 | static int opt_video_codec(OptionsContext *o, const char *opt, const char *arg) |
6291d7e4 | 2589 | { |
35e6f8c1 | 2590 | return parse_option(o, "codec:v", arg, options); |
6291d7e4 AK |
2591 | } |
2592 | ||
35e6f8c1 | 2593 | static int opt_subtitle_codec(OptionsContext *o, const char *opt, const char *arg) |
6291d7e4 | 2594 | { |
35e6f8c1 | 2595 | return parse_option(o, "codec:s", arg, options); |
6291d7e4 AK |
2596 | } |
2597 | ||
35e6f8c1 | 2598 | static int opt_data_codec(OptionsContext *o, const char *opt, const char *arg) |
6291d7e4 | 2599 | { |
35e6f8c1 | 2600 | return parse_option(o, "codec:d", arg, options); |
6291d7e4 AK |
2601 | } |
2602 | ||
575ec4e1 | 2603 | static int opt_map(OptionsContext *o, const char *opt, const char *arg) |
6291d7e4 | 2604 | { |
8d2e4a7e AK |
2605 | StreamMap *m = NULL; |
2606 | int i, negative = 0, file_idx; | |
2607 | int sync_file_idx = -1, sync_stream_idx; | |
2608 | char *p, *sync; | |
2609 | char *map; | |
6291d7e4 | 2610 | |
8d2e4a7e AK |
2611 | if (*arg == '-') { |
2612 | negative = 1; | |
2613 | arg++; | |
2614 | } | |
2615 | map = av_strdup(arg); | |
6291d7e4 | 2616 | |
8d2e4a7e AK |
2617 | /* parse sync stream first, just pick first matching stream */ |
2618 | if (sync = strchr(map, ',')) { | |
2619 | *sync = 0; | |
2620 | sync_file_idx = strtol(sync + 1, &sync, 0); | |
2621 | if (sync_file_idx >= nb_input_files || sync_file_idx < 0) { | |
2622 | av_log(NULL, AV_LOG_ERROR, "Invalid sync file index: %d.\n", sync_file_idx); | |
2623 | exit_program(1); | |
2624 | } | |
2625 | if (*sync) | |
2626 | sync++; | |
ed5b1326 | 2627 | for (i = 0; i < input_files[sync_file_idx].nb_streams; i++) |
8d2e4a7e AK |
2628 | if (check_stream_specifier(input_files[sync_file_idx].ctx, |
2629 | input_files[sync_file_idx].ctx->streams[i], sync) == 1) { | |
2630 | sync_stream_idx = i; | |
2631 | break; | |
2632 | } | |
ed5b1326 | 2633 | if (i == input_files[sync_file_idx].nb_streams) { |
8d2e4a7e AK |
2634 | av_log(NULL, AV_LOG_ERROR, "Sync stream specification in map %s does not " |
2635 | "match any streams.\n", arg); | |
2636 | exit_program(1); | |
2637 | } | |
2638 | } | |
6291d7e4 | 2639 | |
8d2e4a7e AK |
2640 | |
2641 | file_idx = strtol(map, &p, 0); | |
2642 | if (file_idx >= nb_input_files || file_idx < 0) { | |
2643 | av_log(NULL, AV_LOG_ERROR, "Invalid input file index: %d.\n", file_idx); | |
2644 | exit_program(1); | |
6291d7e4 | 2645 | } |
8d2e4a7e AK |
2646 | if (negative) |
2647 | /* disable some already defined maps */ | |
575ec4e1 AK |
2648 | for (i = 0; i < o->nb_stream_maps; i++) { |
2649 | m = &o->stream_maps[i]; | |
8d2e4a7e AK |
2650 | if (check_stream_specifier(input_files[m->file_index].ctx, |
2651 | input_files[m->file_index].ctx->streams[m->stream_index], | |
2652 | *p == ':' ? p + 1 : p) > 0) | |
2653 | m->disabled = 1; | |
2654 | } | |
2655 | else | |
ed5b1326 | 2656 | for (i = 0; i < input_files[file_idx].nb_streams; i++) { |
8d2e4a7e AK |
2657 | if (check_stream_specifier(input_files[file_idx].ctx, input_files[file_idx].ctx->streams[i], |
2658 | *p == ':' ? p + 1 : p) <= 0) | |
2659 | continue; | |
575ec4e1 AK |
2660 | o->stream_maps = grow_array(o->stream_maps, sizeof(*o->stream_maps), |
2661 | &o->nb_stream_maps, o->nb_stream_maps + 1); | |
2662 | m = &o->stream_maps[o->nb_stream_maps - 1]; | |
8d2e4a7e AK |
2663 | |
2664 | m->file_index = file_idx; | |
2665 | m->stream_index = i; | |
2666 | ||
2667 | if (sync_file_idx >= 0) { | |
2668 | m->sync_file_index = sync_file_idx; | |
2669 | m->sync_stream_index = sync_stream_idx; | |
2670 | } else { | |
2671 | m->sync_file_index = file_idx; | |
2672 | m->sync_stream_index = i; | |
2673 | } | |
2674 | } | |
2675 | ||
2676 | if (!m) { | |
2677 | av_log(NULL, AV_LOG_ERROR, "Stream map '%s' matches no streams.\n", arg); | |
2678 | exit_program(1); | |
2679 | } | |
2680 | ||
2681 | av_freep(&map); | |
6291d7e4 AK |
2682 | return 0; |
2683 | } | |
2684 | ||
e6e6060c | 2685 | static void parse_meta_type(char *arg, char *type, int *index) |
6291d7e4 | 2686 | { |
039267f1 | 2687 | if (*arg) { |
a2a38d96 | 2688 | *type = *arg; |
6291d7e4 AK |
2689 | switch (*arg) { |
2690 | case 'g': | |
2691 | break; | |
2692 | case 's': | |
2693 | case 'c': | |
2694 | case 'p': | |
e6e6060c AK |
2695 | if (*(++arg) == ':') |
2696 | *index = strtol(++arg, NULL, 0); | |
6291d7e4 AK |
2697 | break; |
2698 | default: | |
2699 | fprintf(stderr, "Invalid metadata type %c.\n", *arg); | |
2700 | exit_program(1); | |
2701 | } | |
2702 | } else | |
2703 | *type = 'g'; | |
2704 | } | |
2705 | ||
847529f8 | 2706 | static int opt_map_metadata(OptionsContext *o, const char *opt, const char *arg) |
6291d7e4 AK |
2707 | { |
2708 | MetadataMap *m, *m1; | |
2709 | char *p; | |
2710 | ||
847529f8 AK |
2711 | o->meta_data_maps = grow_array(o->meta_data_maps, sizeof(*o->meta_data_maps), |
2712 | &o->nb_meta_data_maps, o->nb_meta_data_maps + 1); | |
6291d7e4 | 2713 | |
847529f8 | 2714 | m = &o->meta_data_maps[o->nb_meta_data_maps - 1][1]; |
6291d7e4 | 2715 | m->file = strtol(arg, &p, 0); |
039267f1 | 2716 | parse_meta_type(*p ? p + 1 : p, &m->type, &m->index); |
6291d7e4 | 2717 | |
847529f8 | 2718 | m1 = &o->meta_data_maps[o->nb_meta_data_maps - 1][0]; |
e6e6060c | 2719 | if (p = strchr(opt, ':')) |
039267f1 | 2720 | parse_meta_type(p + 1, &m1->type, &m1->index); |
e6e6060c AK |
2721 | else |
2722 | m1->type = 'g'; | |
6291d7e4 AK |
2723 | |
2724 | if (m->type == 'g' || m1->type == 'g') | |
847529f8 | 2725 | o->metadata_global_manual = 1; |
6291d7e4 | 2726 | if (m->type == 's' || m1->type == 's') |
847529f8 | 2727 | o->metadata_streams_manual = 1; |
6291d7e4 | 2728 | if (m->type == 'c' || m1->type == 'c') |
847529f8 | 2729 | o->metadata_chapters_manual = 1; |
6291d7e4 AK |
2730 | |
2731 | return 0; | |
2732 | } | |
2733 | ||
169f0647 AK |
2734 | static enum CodecID find_codec_or_die(const char *name, enum AVMediaType type, int encoder) |
2735 | { | |
2736 | const char *codec_string = encoder ? "encoder" : "decoder"; | |
2737 | AVCodec *codec; | |
2738 | ||
2739 | if(!name) | |
2740 | return CODEC_ID_NONE; | |
2741 | codec = encoder ? | |
2742 | avcodec_find_encoder_by_name(name) : | |
2743 | avcodec_find_decoder_by_name(name); | |
2744 | if(!codec) { | |
2745 | av_log(NULL, AV_LOG_ERROR, "Unknown %s '%s'\n", codec_string, name); | |
2746 | exit_program(1); | |
2747 | } | |
2748 | if(codec->type != type) { | |
2749 | av_log(NULL, AV_LOG_ERROR, "Invalid %s type '%s'\n", codec_string, name); | |
2750 | exit_program(1); | |
2751 | } | |
2752 | return codec->id; | |
2753 | } | |
2754 | ||
35e6f8c1 | 2755 | static AVCodec *choose_codec(OptionsContext *o, AVFormatContext *s, AVStream *st, enum AVMediaType type) |
169f0647 | 2756 | { |
169f0647 | 2757 | char *codec_name = NULL; |
169f0647 | 2758 | |
35e6f8c1 | 2759 | MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st); |
169f0647 AK |
2760 | |
2761 | if (!codec_name) { | |
6cd9d0f7 AK |
2762 | if (s->oformat) { |
2763 | st->codec->codec_id = av_guess_codec(s->oformat, NULL, s->filename, NULL, type); | |
2764 | return avcodec_find_encoder(st->codec->codec_id); | |
2765 | } | |
169f0647 AK |
2766 | } else if (!strcmp(codec_name, "copy")) |
2767 | st->stream_copy = 1; | |
2768 | else { | |
2769 | st->codec->codec_id = find_codec_or_die(codec_name, type, s->iformat == NULL); | |
2770 | return s->oformat ? avcodec_find_encoder_by_name(codec_name) : | |
2771 | avcodec_find_decoder_by_name(codec_name); | |
2772 | } | |
2773 | ||
2774 | return NULL; | |
2775 | } | |
6291d7e4 | 2776 | |
88867844 AK |
2777 | /** |
2778 | * Add all the streams from the given input file to the global | |
2779 | * list of input streams. | |
2780 | */ | |
35e6f8c1 | 2781 | static void add_input_streams(OptionsContext *o, AVFormatContext *ic) |
88867844 | 2782 | { |
33f75d72 | 2783 | int i, rfps, rfps_base; |
88867844 AK |
2784 | |
2785 | for (i = 0; i < ic->nb_streams; i++) { | |
2786 | AVStream *st = ic->streams[i]; | |
2787 | AVCodecContext *dec = st->codec; | |
2788 | InputStream *ist; | |
33f75d72 | 2789 | double scale = 1.0; |
88867844 | 2790 | |
88867844 AK |
2791 | input_streams = grow_array(input_streams, sizeof(*input_streams), &nb_input_streams, nb_input_streams + 1); |
2792 | ist = &input_streams[nb_input_streams - 1]; | |
2793 | ist->st = st; | |
2794 | ist->file_index = nb_input_files; | |
2795 | ist->discard = 1; | |
2796 | ist->opts = filter_codec_opts(codec_opts, ist->st->codec->codec_id, ic, st); | |
2797 | ||
33f75d72 AK |
2798 | MATCH_PER_STREAM_OPT(ts_scale, dbl, scale, ic, st); |
2799 | ist->ts_scale = scale; | |
88867844 | 2800 | |
35e6f8c1 | 2801 | ist->dec = choose_codec(o, ic, st, dec->codec_type); |
c74b44de AK |
2802 | if (!ist->dec) |
2803 | ist->dec = avcodec_find_decoder(dec->codec_id); | |
88867844 AK |
2804 | |
2805 | switch (dec->codec_type) { | |
2806 | case AVMEDIA_TYPE_AUDIO: | |
2130981a | 2807 | if (o->audio_disable) |
88867844 AK |
2808 | st->discard= AVDISCARD_ALL; |
2809 | break; | |
2810 | case AVMEDIA_TYPE_VIDEO: | |
2811 | rfps = ic->streams[i]->r_frame_rate.num; | |
2812 | rfps_base = ic->streams[i]->r_frame_rate.den; | |
2813 | if (dec->lowres) { | |
2814 | dec->flags |= CODEC_FLAG_EMU_EDGE; | |
2815 | dec->height >>= dec->lowres; | |
2816 | dec->width >>= dec->lowres; | |
2817 | } | |
2818 | if(me_threshold) | |
2819 | dec->debug |= FF_DEBUG_MV; | |
2820 | ||
2821 | if (dec->time_base.den != rfps*dec->ticks_per_frame || dec->time_base.num != rfps_base) { | |
2822 | ||
2823 | if (verbose >= 0) | |
2824 | fprintf(stderr,"\nSeems stream %d codec frame rate differs from container frame rate: %2.2f (%d/%d) -> %2.2f (%d/%d)\n", | |
2825 | i, (float)dec->time_base.den / dec->time_base.num, dec->time_base.den, dec->time_base.num, | |
2826 | ||
2827 | (float)rfps / rfps_base, rfps, rfps_base); | |
2828 | } | |
2829 | ||
2130981a | 2830 | if (o->video_disable) |
88867844 AK |
2831 | st->discard= AVDISCARD_ALL; |
2832 | else if(video_discard) | |
2833 | st->discard= video_discard; | |
2834 | break; | |
2835 | case AVMEDIA_TYPE_DATA: | |
2836 | break; | |
2837 | case AVMEDIA_TYPE_SUBTITLE: | |
2130981a | 2838 | if (o->subtitle_disable) |
88867844 AK |
2839 | st->discard = AVDISCARD_ALL; |
2840 | break; | |
2841 | case AVMEDIA_TYPE_ATTACHMENT: | |
2842 | case AVMEDIA_TYPE_UNKNOWN: | |
2843 | break; | |
2844 | default: | |
2845 | abort(); | |
2846 | } | |
2847 | } | |
2848 | } | |
2849 | ||
575ec4e1 | 2850 | static int opt_input_file(OptionsContext *o, const char *opt, const char *filename) |
6291d7e4 AK |
2851 | { |
2852 | AVFormatContext *ic; | |
2853 | AVInputFormat *file_iformat = NULL; | |
88867844 | 2854 | int err, i, ret; |
6291d7e4 AK |
2855 | int64_t timestamp; |
2856 | uint8_t buf[128]; | |
2857 | AVDictionary **opts; | |
2858 | int orig_nb_streams; // number of streams before avformat_find_stream_info | |
2859 | ||
7041bb3b AK |
2860 | if (o->format) { |
2861 | if (!(file_iformat = av_find_input_format(o->format))) { | |
2862 | fprintf(stderr, "Unknown input format: '%s'\n", o->format); | |
6291d7e4 AK |
2863 | exit_program(1); |
2864 | } | |
6291d7e4 AK |
2865 | } |
2866 | ||
2867 | if (!strcmp(filename, "-")) | |
2868 | filename = "pipe:"; | |
2869 | ||
2870 | using_stdin |= !strncmp(filename, "pipe:", 5) || | |
2871 | !strcmp(filename, "/dev/stdin"); | |
2872 | ||
2873 | /* get default parameters from command line */ | |
2874 | ic = avformat_alloc_context(); | |
2875 | if (!ic) { | |
2876 | print_error(filename, AVERROR(ENOMEM)); | |
2877 | exit_program(1); | |
2878 | } | |
e2469ccf AK |
2879 | if (o->nb_audio_sample_rate) { |
2880 | snprintf(buf, sizeof(buf), "%d", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i); | |
6291d7e4 AK |
2881 | av_dict_set(&format_opts, "sample_rate", buf, 0); |
2882 | } | |
6a11686d AK |
2883 | if (o->nb_audio_channels) { |
2884 | snprintf(buf, sizeof(buf), "%d", o->audio_channels[o->nb_audio_channels - 1].u.i); | |
6291d7e4 AK |
2885 | av_dict_set(&format_opts, "channels", buf, 0); |
2886 | } | |
2887 | if (frame_rate.num) { | |
2888 | snprintf(buf, sizeof(buf), "%d/%d", frame_rate.num, frame_rate.den); | |
2889 | av_dict_set(&format_opts, "framerate", buf, 0); | |
2890 | } | |
2891 | if (frame_width && frame_height) { | |
2892 | snprintf(buf, sizeof(buf), "%dx%d", frame_width, frame_height); | |
2893 | av_dict_set(&format_opts, "video_size", buf, 0); | |
2894 | } | |
2895 | if (frame_pix_fmt != PIX_FMT_NONE) | |
2896 | av_dict_set(&format_opts, "pixel_format", av_get_pix_fmt_name(frame_pix_fmt), 0); | |
2897 | ||
6291d7e4 AK |
2898 | ic->flags |= AVFMT_FLAG_NONBLOCK; |
2899 | ||
2900 | /* open the input file with generic libav function */ | |
2901 | err = avformat_open_input(&ic, filename, file_iformat, &format_opts); | |
2902 | if (err < 0) { | |
2903 | print_error(filename, err); | |
2904 | exit_program(1); | |
2905 | } | |
2906 | assert_avoptions(format_opts); | |
2907 | ||
92f1940e AK |
2908 | /* apply forced codec ids */ |
2909 | for (i = 0; i < ic->nb_streams; i++) | |
35e6f8c1 | 2910 | choose_codec(o, ic, ic->streams[i], ic->streams[i]->codec->codec_type); |
92f1940e | 2911 | |
6291d7e4 AK |
2912 | /* Set AVCodecContext options for avformat_find_stream_info */ |
2913 | opts = setup_find_stream_info_opts(ic, codec_opts); | |
2914 | orig_nb_streams = ic->nb_streams; | |
2915 | ||
2916 | /* If not enough info to get the stream parameters, we decode the | |
2917 | first frames to get it. (used in mpeg case for example) */ | |
2918 | ret = avformat_find_stream_info(ic, opts); | |
2919 | if (ret < 0 && verbose >= 0) { | |
2920 | fprintf(stderr, "%s: could not find codec parameters\n", filename); | |
2921 | av_close_input_file(ic); | |
2922 | exit_program(1); | |
2923 | } | |
2924 | ||
6b779ccc | 2925 | timestamp = o->start_time; |
6291d7e4 AK |
2926 | /* add the stream start time */ |
2927 | if (ic->start_time != AV_NOPTS_VALUE) | |
2928 | timestamp += ic->start_time; | |
2929 | ||
2930 | /* if seeking requested, we execute it */ | |
6b779ccc | 2931 | if (o->start_time != 0) { |
6291d7e4 AK |
2932 | ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD); |
2933 | if (ret < 0) { | |
2934 | fprintf(stderr, "%s: could not seek to position %0.3f\n", | |
2935 | filename, (double)timestamp / AV_TIME_BASE); | |
2936 | } | |
6291d7e4 AK |
2937 | } |
2938 | ||
2939 | /* update the current parameters so that they match the one of the input stream */ | |
35e6f8c1 | 2940 | add_input_streams(o, ic); |
6291d7e4 AK |
2941 | |
2942 | /* dump the file content */ | |
2943 | if (verbose >= 0) | |
2944 | av_dump_format(ic, nb_input_files, filename, 0); | |
2945 | ||
2946 | input_files = grow_array(input_files, sizeof(*input_files), &nb_input_files, nb_input_files + 1); | |
2947 | input_files[nb_input_files - 1].ctx = ic; | |
2948 | input_files[nb_input_files - 1].ist_index = nb_input_streams - ic->nb_streams; | |
6b779ccc | 2949 | input_files[nb_input_files - 1].ts_offset = o->input_ts_offset - (copy_ts ? 0 : timestamp); |
ed5b1326 | 2950 | input_files[nb_input_files - 1].nb_streams = ic->nb_streams; |
6291d7e4 AK |
2951 | |
2952 | frame_rate = (AVRational){0, 0}; | |
2953 | frame_pix_fmt = PIX_FMT_NONE; | |
2954 | frame_height = 0; | |
2955 | frame_width = 0; | |
6291d7e4 AK |
2956 | |
2957 | for (i = 0; i < orig_nb_streams; i++) | |
2958 | av_dict_free(&opts[i]); | |
2959 | av_freep(&opts); | |
575ec4e1 AK |
2960 | |
2961 | reset_options(o); | |
6291d7e4 AK |
2962 | return 0; |
2963 | } | |
2964 | ||
f233cfed AK |
2965 | static void parse_forced_key_frames(char *kf, OutputStream *ost, |
2966 | AVCodecContext *avctx) | |
2967 | { | |
2968 | char *p; | |
2969 | int n = 1, i; | |
2970 | int64_t t; | |
2971 | ||
2972 | for (p = kf; *p; p++) | |
2973 | if (*p == ',') | |
2974 | n++; | |
2975 | ost->forced_kf_count = n; | |
2976 | ost->forced_kf_pts = av_malloc(sizeof(*ost->forced_kf_pts) * n); | |
2977 | if (!ost->forced_kf_pts) { | |
2978 | av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n"); | |
2979 | exit_program(1); | |
2980 | } | |
2981 | for (i = 0; i < n; i++) { | |
2982 | p = i ? strchr(p, ',') + 1 : kf; | |
2983 | t = parse_time_or_die("force_key_frames", p, 1); | |
2984 | ost->forced_kf_pts[i] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base); | |
2985 | } | |
2986 | } | |
2987 | ||
35e6f8c1 | 2988 | static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type) |
169f0647 AK |
2989 | { |
2990 | OutputStream *ost; | |
2991 | AVStream *st = av_new_stream(oc, oc->nb_streams < nb_streamid_map ? streamid_map[oc->nb_streams] : 0); | |
2992 | int idx = oc->nb_streams - 1; | |
96139b5e | 2993 | int64_t max_frames = INT64_MAX; |
013887eb | 2994 | char *bsf = NULL, *next, *codec_tag = NULL; |
d821cbe2 | 2995 | AVBitStreamFilterContext *bsfc, *bsfc_prev = NULL; |
77d9c454 | 2996 | double qscale = -1; |
169f0647 AK |
2997 | |
2998 | if (!st) { | |
2999 | av_log(NULL, AV_LOG_ERROR, "Could not alloc stream.\n"); | |
3000 | exit_program(1); | |
3001 | } | |
3002 | ||
4288e031 AK |
3003 | output_streams = grow_array(output_streams, sizeof(*output_streams), &nb_output_streams, |
3004 | nb_output_streams + 1); | |
3005 | ost = &output_streams[nb_output_streams - 1]; | |
40fc2810 | 3006 | ost->file_index = nb_output_files; |
169f0647 AK |
3007 | ost->index = idx; |
3008 | ost->st = st; | |
3009 | st->codec->codec_type = type; | |
35e6f8c1 | 3010 | ost->enc = choose_codec(o, oc, st, type); |
169f0647 AK |
3011 | if (ost->enc) { |
3012 | ost->opts = filter_codec_opts(codec_opts, ost->enc->id, oc, st); | |
3013 | } | |
3014 | ||
3015 | avcodec_get_context_defaults3(st->codec, ost->enc); | |
3016 | st->codec->codec_type = type; // XXX hack, avcodec_get_context_defaults2() sets type to unknown for stream copy | |
3017 | ||
96139b5e AK |
3018 | MATCH_PER_STREAM_OPT(max_frames, i64, max_frames, oc, st); |
3019 | ost->max_frames = max_frames; | |
3020 | ||
d821cbe2 AK |
3021 | MATCH_PER_STREAM_OPT(bitstream_filters, str, bsf, oc, st); |
3022 | while (bsf) { | |
3023 | if (next = strchr(bsf, ',')) | |
3024 | *next++ = 0; | |
3025 | if (!(bsfc = av_bitstream_filter_init(bsf))) { | |
3026 | av_log(NULL, AV_LOG_ERROR, "Unknown bitstream filter %s\n", bsf); | |
3027 | exit_program(1); | |
3028 | } | |
3029 | if (bsfc_prev) | |
3030 | bsfc_prev->next = bsfc; | |
3031 | else | |
3032 | ost->bitstream_filters = bsfc; | |
3033 | ||
3034 | bsfc_prev = bsfc; | |
3035 | bsf = next; | |
3036 | } | |
3037 | ||
013887eb AK |
3038 | MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st); |
3039 | if (codec_tag) { | |
3040 | uint32_t tag = strtol(codec_tag, &next, 0); | |
3041 | if (*next) | |
3042 | tag = AV_RL32(codec_tag); | |
3043 | st->codec->codec_tag = tag; | |
3044 | } | |
3045 | ||
77d9c454 AK |
3046 | MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st); |
3047 | if (qscale >= 0 || same_quant) { | |
3048 | st->codec->flags |= CODEC_FLAG_QSCALE; | |
3049 | st->codec->global_quality = FF_QP2LAMBDA * qscale; | |
3050 | } | |
3051 | ||
169f0647 AK |
3052 | ost->sws_flags = av_get_int(sws_opts, "sws_flags", NULL); |
3053 | return ost; | |
3054 | } | |
3055 | ||
35e6f8c1 | 3056 | static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc) |
6291d7e4 AK |
3057 | { |
3058 | AVStream *st; | |
3059 | OutputStream *ost; | |
3060 | AVCodecContext *video_enc; | |
6291d7e4 | 3061 | |
35e6f8c1 | 3062 | ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO); |
6291d7e4 | 3063 | st = ost->st; |
92f1940e | 3064 | if (!st->stream_copy) { |
6291d7e4 AK |
3065 | ost->frame_aspect_ratio = frame_aspect_ratio; |
3066 | frame_aspect_ratio = 0; | |
3067 | #if CONFIG_AVFILTER | |
3068 | ost->avfilter= vfilters; | |
3069 | vfilters = NULL; | |
3070 | #endif | |
3071 | } | |
3072 | ||
6291d7e4 AK |
3073 | video_enc = st->codec; |
3074 | ||
6291d7e4 AK |
3075 | if(oc->oformat->flags & AVFMT_GLOBALHEADER) { |
3076 | video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER; | |
3077 | } | |
3078 | ||
92f1940e | 3079 | if (st->stream_copy) { |
6291d7e4 AK |
3080 | video_enc->sample_aspect_ratio = |
3081 | st->sample_aspect_ratio = av_d2q(frame_aspect_ratio*frame_height/frame_width, 255); | |
3082 | } else { | |
3083 | const char *p; | |
7c029672 | 3084 | char *forced_key_frames = NULL; |
bef737a7 | 3085 | int i, force_fps = 0; |
6291d7e4 AK |
3086 | |
3087 | if (frame_rate.num) | |
3088 | ost->frame_rate = frame_rate; | |
6291d7e4 AK |
3089 | |
3090 | video_enc->width = frame_width; | |
3091 | video_enc->height = frame_height; | |
3092 | video_enc->pix_fmt = frame_pix_fmt; | |
3093 | st->sample_aspect_ratio = video_enc->sample_aspect_ratio; | |
3094 | ||
6291d7e4 AK |
3095 | if(intra_matrix) |
3096 | video_enc->intra_matrix = intra_matrix; | |
3097 | if(inter_matrix) | |
3098 | video_enc->inter_matrix = inter_matrix; | |
3099 | ||
3100 | p= video_rc_override_string; | |
3101 | for(i=0; p; i++){ | |
3102 | int start, end, q; | |
3103 | int e=sscanf(p, "%d,%d,%d", &start, &end, &q); | |
3104 | if(e!=3){ | |
3105 | fprintf(stderr, "error parsing rc_override\n"); | |
3106 | exit_program(1); | |
3107 | } | |
3108 | video_enc->rc_override= | |
3109 | av_realloc(video_enc->rc_override, | |
3110 | sizeof(RcOverride)*(i+1)); | |
3111 | video_enc->rc_override[i].start_frame= start; | |
3112 | video_enc->rc_override[i].end_frame = end; | |
3113 | if(q>0){ | |
3114 | video_enc->rc_override[i].qscale= q; | |
3115 | video_enc->rc_override[i].quality_factor= 1.0; | |
3116 | } | |
3117 | else{ | |
3118 | video_enc->rc_override[i].qscale= 0; | |
3119 | video_enc->rc_override[i].quality_factor= -q/100.0; | |
3120 | } | |
3121 | p= strchr(p, '/'); | |
3122 | if(p) p++; | |
3123 | } | |
3124 | video_enc->rc_override_count=i; | |
3125 | if (!video_enc->rc_initial_buffer_occupancy) | |
3126 | video_enc->rc_initial_buffer_occupancy = video_enc->rc_buffer_size*3/4; | |
3127 | video_enc->me_threshold= me_threshold; | |
3128 | video_enc->intra_dc_precision= intra_dc_precision - 8; | |
3129 | ||
3130 | if (do_psnr) | |
3131 | video_enc->flags|= CODEC_FLAG_PSNR; | |
3132 | ||
3133 | /* two pass mode */ | |
3134 | if (do_pass) { | |
3135 | if (do_pass == 1) { | |
3136 | video_enc->flags |= CODEC_FLAG_PASS1; | |
3137 | } else { | |
3138 | video_enc->flags |= CODEC_FLAG_PASS2; | |
3139 | } | |
3140 | } | |
3141 | ||
7c029672 | 3142 | MATCH_PER_STREAM_OPT(forced_key_frames, str, forced_key_frames, oc, st); |
6291d7e4 AK |
3143 | if (forced_key_frames) |
3144 | parse_forced_key_frames(forced_key_frames, ost, video_enc); | |
bef737a7 AK |
3145 | |
3146 | MATCH_PER_STREAM_OPT(force_fps, i, force_fps, oc, st); | |
3147 | ost->force_fps = force_fps; | |
6291d7e4 | 3148 | } |
6291d7e4 AK |
3149 | |
3150 | /* reset some key parameters */ | |
6291d7e4 | 3151 | frame_pix_fmt = PIX_FMT_NONE; |
3d4f0dab | 3152 | return ost; |
6291d7e4 AK |
3153 | } |
3154 | ||
35e6f8c1 | 3155 | static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc) |
6291d7e4 AK |
3156 | { |
3157 | AVStream *st; | |
3158 | OutputStream *ost; | |
6291d7e4 | 3159 | AVCodecContext *audio_enc; |
6291d7e4 | 3160 | |
35e6f8c1 | 3161 | ost = new_output_stream(o, oc, AVMEDIA_TYPE_AUDIO); |
6291d7e4 AK |
3162 | st = ost->st; |
3163 | ||
6291d7e4 AK |
3164 | audio_enc = st->codec; |
3165 | audio_enc->codec_type = AVMEDIA_TYPE_AUDIO; | |
3166 | ||
6291d7e4 AK |
3167 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) { |
3168 | audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER; | |
3169 | } | |
92f1940e | 3170 | if (!st->stream_copy) { |
05bffc12 AK |
3171 | char *sample_fmt = NULL; |
3172 | ||
6a11686d AK |
3173 | MATCH_PER_STREAM_OPT(audio_channels, i, audio_enc->channels, oc, st); |
3174 | ||
05bffc12 AK |
3175 | MATCH_PER_STREAM_OPT(sample_fmts, str, sample_fmt, oc, st); |
3176 | if (sample_fmt && | |
3177 | (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) { | |
3178 | av_log(NULL, AV_LOG_ERROR, "Invalid sample format '%s'\n", sample_fmt); | |
3179 | exit_program(1); | |
3180 | } | |
3181 | ||
e2469ccf | 3182 | MATCH_PER_STREAM_OPT(audio_sample_rate, i, audio_enc->sample_rate, oc, st); |
6291d7e4 | 3183 | } |
6291d7e4 | 3184 | |
3d4f0dab | 3185 | return ost; |
6291d7e4 AK |
3186 | } |
3187 | ||
35e6f8c1 | 3188 | static OutputStream *new_data_stream(OptionsContext *o, AVFormatContext *oc) |
6291d7e4 AK |
3189 | { |
3190 | AVStream *st; | |
3191 | OutputStream *ost; | |
3192 | AVCodecContext *data_enc; | |
3193 | ||
35e6f8c1 | 3194 | ost = new_output_stream(o, oc, AVMEDIA_TYPE_DATA); |
6291d7e4 AK |
3195 | st = ost->st; |
3196 | data_enc = st->codec; | |
92f1940e | 3197 | if (!st->stream_copy) { |
6291d7e4 AK |
3198 | fprintf(stderr, "Data stream encoding not supported yet (only streamcopy)\n"); |
3199 | exit_program(1); | |
3200 | } | |
3201 | ||
6291d7e4 AK |
3202 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) { |
3203 | data_enc->flags |= CODEC_FLAG_GLOBAL_HEADER; | |
3204 | } | |
6291d7e4 | 3205 | |
3d4f0dab | 3206 | return ost; |
6291d7e4 AK |
3207 | } |
3208 | ||
35e6f8c1 | 3209 | static OutputStream *new_subtitle_stream(OptionsContext *o, AVFormatContext *oc) |
6291d7e4 AK |
3210 | { |
3211 | AVStream *st; | |
3212 | OutputStream *ost; | |
6291d7e4 | 3213 | AVCodecContext *subtitle_enc; |
6291d7e4 | 3214 | |
35e6f8c1 | 3215 | ost = new_output_stream(o, oc, AVMEDIA_TYPE_SUBTITLE); |
6291d7e4 AK |
3216 | st = ost->st; |
3217 | subtitle_enc = st->codec; | |
3218 | ||
6291d7e4 AK |
3219 | subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE; |
3220 | ||
6291d7e4 AK |
3221 | if (oc->oformat->flags & AVFMT_GLOBALHEADER) { |
3222 | subtitle_enc->flags |= CODEC_FLAG_GLOBAL_HEADER; | |
3223 | } | |
6291d7e4 | 3224 | |
3d4f0dab | 3225 | return ost; |
6291d7e4 AK |
3226 | } |
3227 | ||
3228 | /* arg format is "output-stream-index:streamid-value". */ | |
3229 | static int opt_streamid(const char *opt, const char *arg) | |
3230 | { | |
3231 | int idx; | |
3232 | char *p; | |
3233 | char idx_str[16]; | |
3234 | ||
3235 | av_strlcpy(idx_str, arg, sizeof(idx_str)); | |
3236 | p = strchr(idx_str, ':'); | |
3237 | if (!p) { | |
3238 | fprintf(stderr, | |
3239 | "Invalid value '%s' for option '%s', required syntax is 'index:value'\n", | |
3240 | arg, opt); | |
3241 | exit_program(1); | |
3242 | } | |
3243 | *p++ = '\0'; | |
3244 | idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, INT_MAX); | |
3245 | streamid_map = grow_array(streamid_map, sizeof(*streamid_map), &nb_streamid_map, idx+1); | |
3246 | streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX); | |
3247 | return 0; | |
3248 | } | |
3249 | ||
847529f8 | 3250 | static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata) |
86530f41 | 3251 | { |
6b779ccc AK |
3252 | AVFormatContext *is = ifile->ctx; |
3253 | AVFormatContext *os = ofile->ctx; | |
86530f41 AK |
3254 | int i; |
3255 | ||
3256 | for (i = 0; i < is->nb_chapters; i++) { | |
3257 | AVChapter *in_ch = is->chapters[i], *out_ch; | |
6b779ccc | 3258 | int64_t ts_off = av_rescale_q(ofile->start_time - ifile->ts_offset, |
86530f41 | 3259 | AV_TIME_BASE_Q, in_ch->time_base); |
6b779ccc AK |
3260 | int64_t rt = (ofile->recording_time == INT64_MAX) ? INT64_MAX : |
3261 | av_rescale_q(ofile->recording_time, AV_TIME_BASE_Q, in_ch->time_base); | |
86530f41 AK |
3262 | |
3263 | ||
3264 | if (in_ch->end < ts_off) | |
3265 | continue; | |
3266 | if (rt != INT64_MAX && in_ch->start > rt + ts_off) | |
3267 | break; | |
3268 | ||
3269 | out_ch = av_mallocz(sizeof(AVChapter)); | |
3270 | if (!out_ch) | |
3271 | return AVERROR(ENOMEM); | |
3272 | ||
3273 | out_ch->id = in_ch->id; | |
3274 | out_ch->time_base = in_ch->time_base; | |
3275 | out_ch->start = FFMAX(0, in_ch->start - ts_off); | |
3276 | out_ch->end = FFMIN(rt, in_ch->end - ts_off); | |
3277 | ||
847529f8 | 3278 | if (copy_metadata) |
86530f41 AK |
3279 | av_dict_copy(&out_ch->metadata, in_ch->metadata, 0); |
3280 | ||
3281 | os->nb_chapters++; | |
3282 | os->chapters = av_realloc(os->chapters, sizeof(AVChapter)*os->nb_chapters); | |
3283 | if (!os->chapters) | |
3284 | return AVERROR(ENOMEM); | |
3285 | os->chapters[os->nb_chapters - 1] = out_ch; | |
3286 | } | |
3287 | return 0; | |
3288 | } | |
3289 | ||
35e6f8c1 | 3290 | static int read_avserver_streams(OptionsContext *o, AVFormatContext *s, const char *filename) |
169f0647 AK |
3291 | { |
3292 | int i, err; | |
3293 | AVFormatContext *ic = NULL; | |
169f0647 AK |
3294 | |
3295 | err = avformat_open_input(&ic, filename, NULL, NULL); | |
3296 | if (err < 0) | |
3297 | return err; | |
3298 | /* copy stream format */ | |
3299 | for(i=0;i<ic->nb_streams;i++) { | |
3300 | AVStream *st; | |
3301 | OutputStream *ost; | |
3302 | AVCodec *codec; | |
3303 | ||
3304 | codec = avcodec_find_encoder(ic->streams[i]->codec->codec_id); | |
35e6f8c1 | 3305 | ost = new_output_stream(o, s, codec->type); |
169f0647 AK |
3306 | st = ost->st; |
3307 | ||
3308 | // FIXME: a more elegant solution is needed | |
3309 | memcpy(st, ic->streams[i], sizeof(AVStream)); | |
3310 | st->info = NULL; | |
3311 | avcodec_copy_context(st->codec, ic->streams[i]->codec); | |
3312 | ||
3313 | if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && !st->stream_copy) | |
3314 | choose_sample_fmt(st, codec); | |
3315 | else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && !st->stream_copy) | |
3316 | choose_pixel_fmt(st, codec); | |
169f0647 AK |
3317 | } |
3318 | ||
3319 | av_close_input_file(ic); | |
3320 | return 0; | |
3321 | } | |
3322 | ||
7cc8d638 | 3323 | static void opt_output_file(void *optctx, const char *filename) |
6291d7e4 | 3324 | { |
575ec4e1 | 3325 | OptionsContext *o = optctx; |
6291d7e4 | 3326 | AVFormatContext *oc; |
3d4f0dab | 3327 | int i, err; |
6291d7e4 | 3328 | AVOutputFormat *file_oformat; |
3d4f0dab AK |
3329 | OutputStream *ost; |
3330 | InputStream *ist; | |
6291d7e4 AK |
3331 | |
3332 | if (!strcmp(filename, "-")) | |
3333 | filename = "pipe:"; | |
3334 | ||
3335 | oc = avformat_alloc_context(); | |
3336 | if (!oc) { | |
3337 | print_error(filename, AVERROR(ENOMEM)); | |
3338 | exit_program(1); | |
3339 | } | |
3340 | ||
7041bb3b AK |
3341 | if (o->format) { |
3342 | file_oformat = av_guess_format(o->format, NULL, NULL); | |
6291d7e4 | 3343 | if (!file_oformat) { |
7041bb3b | 3344 | fprintf(stderr, "Requested output format '%s' is not a suitable output format\n", o->format); |
6291d7e4 AK |
3345 | exit_program(1); |
3346 | } | |
6291d7e4 AK |
3347 | } else { |
3348 | file_oformat = av_guess_format(NULL, filename, NULL); | |
3349 | if (!file_oformat) { | |
3350 | fprintf(stderr, "Unable to find a suitable output format for '%s'\n", | |
3351 | filename); | |
3352 | exit_program(1); | |
3353 | } | |
3354 | } | |
3355 | ||
3356 | oc->oformat = file_oformat; | |
3357 | av_strlcpy(oc->filename, filename, sizeof(oc->filename)); | |
3358 | ||
3359 | if (!strcmp(file_oformat->name, "ffm") && | |
3360 | av_strstart(filename, "http:", NULL)) { | |
3361 | /* special case for files sent to avserver: we get the stream | |
3362 | parameters from avserver */ | |
35e6f8c1 | 3363 | int err = read_avserver_streams(o, oc, filename); |
6291d7e4 AK |
3364 | if (err < 0) { |
3365 | print_error(filename, err); | |
3366 | exit_program(1); | |
3367 | } | |
575ec4e1 | 3368 | } else if (!o->nb_stream_maps) { |
3d4f0dab AK |
3369 | /* pick the "best" stream of each type */ |
3370 | #define NEW_STREAM(type, index)\ | |
3371 | if (index >= 0) {\ | |
35e6f8c1 | 3372 | ost = new_ ## type ## _stream(o, oc);\ |
3d4f0dab AK |
3373 | ost->source_index = index;\ |
3374 | ost->sync_ist = &input_streams[index];\ | |
3375 | input_streams[index].discard = 0;\ | |
3376 | } | |
3377 | ||
3378 | /* video: highest resolution */ | |
2130981a | 3379 | if (!o->video_disable && oc->oformat->video_codec != CODEC_ID_NONE) { |
3d4f0dab AK |
3380 | int area = 0, idx = -1; |
3381 | for (i = 0; i < nb_input_streams; i++) { | |
3382 | ist = &input_streams[i]; | |
3383 | if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && | |
3384 | ist->st->codec->width * ist->st->codec->height > area) { | |
3385 | area = ist->st->codec->width * ist->st->codec->height; | |
3386 | idx = i; | |
3387 | } | |
3388 | } | |
3389 | NEW_STREAM(video, idx); | |
3390 | } | |
3391 | ||
3392 | /* audio: most channels */ | |
2130981a | 3393 | if (!o->audio_disable && oc->oformat->audio_codec != CODEC_ID_NONE) { |
3d4f0dab AK |
3394 | int channels = 0, idx = -1; |
3395 | for (i = 0; i < nb_input_streams; i++) { | |
3396 | ist = &input_streams[i]; | |
3397 | if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && | |
3398 | ist->st->codec->channels > channels) { | |
3399 | channels = ist->st->codec->channels; | |
3400 | idx = i; | |
3401 | } | |
3402 | } | |
3403 | NEW_STREAM(audio, idx); | |
3404 | } | |
3405 | ||
3406 | /* subtitles: pick first */ | |
2130981a | 3407 | if (!o->subtitle_disable && oc->oformat->subtitle_codec != CODEC_ID_NONE) { |
3d4f0dab AK |
3408 | for (i = 0; i < nb_input_streams; i++) |
3409 | if (input_streams[i].st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) { | |
3410 | NEW_STREAM(subtitle, i); | |
3411 | break; | |
3412 | } | |
3413 | } | |
3414 | /* do something with data? */ | |
6291d7e4 | 3415 | } else { |
575ec4e1 AK |
3416 | for (i = 0; i < o->nb_stream_maps; i++) { |
3417 | StreamMap *map = &o->stream_maps[i]; | |
3d4f0dab | 3418 | |
8d2e4a7e AK |
3419 | if (map->disabled) |
3420 | continue; | |
3d4f0dab | 3421 | |
6cd9d0f7 | 3422 | ist = &input_streams[input_files[map->file_index].ist_index + map->stream_index]; |
3d4f0dab | 3423 | switch (ist->st->codec->codec_type) { |
35e6f8c1 AK |
3424 | case AVMEDIA_TYPE_VIDEO: ost = new_video_stream(o, oc); break; |
3425 | case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream(o, oc); break; | |
3426 | case AVMEDIA_TYPE_SUBTITLE: ost = new_subtitle_stream(o, oc); break; | |
3427 | case AVMEDIA_TYPE_DATA: ost = new_data_stream(o, oc); break; | |
3d4f0dab AK |
3428 | default: |
3429 | av_log(NULL, AV_LOG_ERROR, "Cannot map stream #%d.%d - unsupported type.\n", | |
3430 | map->file_index, map->stream_index); | |
3431 | exit_program(1); | |
3432 | } | |
3433 | ||
6cd9d0f7 | 3434 | ost->source_index = input_files[map->file_index].ist_index + map->stream_index; |
3d4f0dab AK |
3435 | ost->sync_ist = &input_streams[input_files[map->sync_file_index].ist_index + |
3436 | map->sync_stream_index]; | |
3437 | ist->discard = 0; | |
3438 | } | |
6291d7e4 AK |
3439 | } |
3440 | ||
af70aa45 AK |
3441 | output_files = grow_array(output_files, sizeof(*output_files), &nb_output_files, nb_output_files + 1); |
3442 | output_files[nb_output_files - 1].ctx = oc; | |
4288e031 | 3443 | output_files[nb_output_files - 1].ost_index = nb_output_streams - oc->nb_streams; |
6b779ccc AK |
3444 | output_files[nb_output_files - 1].recording_time = o->recording_time; |
3445 | output_files[nb_output_files - 1].start_time = o->start_time; | |
13ccba50 | 3446 | output_files[nb_output_files - 1].limit_filesize = o->limit_filesize; |
af70aa45 | 3447 | av_dict_copy(&output_files[nb_output_files - 1].opts, format_opts, 0); |
6291d7e4 AK |
3448 | |
3449 | /* check filename in case of an image number is expected */ | |
3450 | if (oc->oformat->flags & AVFMT_NEEDNUMBER) { | |
3451 | if (!av_filename_number_test(oc->filename)) { | |
3452 | print_error(oc->filename, AVERROR(EINVAL)); | |
3453 | exit_program(1); | |
3454 | } | |
3455 | } | |
3456 | ||
3457 | if (!(oc->oformat->flags & AVFMT_NOFILE)) { | |
3458 | /* test if it already exists to avoid loosing precious files */ | |
3459 | if (!file_overwrite && | |
3460 | (strchr(filename, ':') == NULL || | |
3461 | filename[1] == ':' || | |
3462 | av_strstart(filename, "file:", NULL))) { | |
3463 | if (avio_check(filename, 0) == 0) { | |
3464 | if (!using_stdin) { | |
3465 | fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename); | |
3466 | fflush(stderr); | |
3467 | if (!read_yesno()) { | |
3468 | fprintf(stderr, "Not overwriting - exiting\n"); | |
3469 | exit_program(1); | |
3470 | } | |
3471 | } | |
3472 | else { | |
3473 | fprintf(stderr,"File '%s' already exists. Exiting.\n", filename); | |
3474 | exit_program(1); | |
3475 | } | |
3476 | } | |
3477 | } | |
3478 | ||
3479 | /* open the file */ | |
3480 | if ((err = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE)) < 0) { | |
3481 | print_error(filename, err); | |
3482 | exit_program(1); | |
3483 | } | |
3484 | } | |
3485 | ||
dc26318c AK |
3486 | oc->preload = (int)(o->mux_preload * AV_TIME_BASE); |
3487 | oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE); | |
6291d7e4 AK |
3488 | oc->flags |= AVFMT_FLAG_NONBLOCK; |
3489 | ||
b9aac90b | 3490 | /* copy chapters */ |
c5bb372e AK |
3491 | if (o->chapters_input_file >= nb_input_files) { |
3492 | if (o->chapters_input_file == INT_MAX) { | |
b9aac90b | 3493 | /* copy chapters from the first input file that has them*/ |
c5bb372e | 3494 | o->chapters_input_file = -1; |
b9aac90b AK |
3495 | for (i = 0; i < nb_input_files; i++) |
3496 | if (input_files[i].ctx->nb_chapters) { | |
c5bb372e | 3497 | o->chapters_input_file = i; |
b9aac90b AK |
3498 | break; |
3499 | } | |
3500 | } else { | |
3501 | av_log(NULL, AV_LOG_ERROR, "Invalid input file index %d in chapter mapping.\n", | |
c5bb372e | 3502 | o->chapters_input_file); |
b9aac90b AK |
3503 | exit_program(1); |
3504 | } | |
3505 | } | |
c5bb372e AK |
3506 | if (o->chapters_input_file >= 0) |
3507 | copy_chapters(&input_files[o->chapters_input_file], &output_files[nb_output_files - 1], | |
847529f8 | 3508 | o->metadata_chapters_manual); |
b9aac90b | 3509 | |
e6e6060c | 3510 | /* copy metadata */ |
847529f8 | 3511 | for (i = 0; i < o->nb_meta_data_maps; i++) { |
e6e6060c AK |
3512 | AVFormatContext *files[2]; |
3513 | AVDictionary **meta[2]; | |
3514 | int j; | |
3515 | ||
3516 | #define METADATA_CHECK_INDEX(index, nb_elems, desc)\ | |
3517 | if ((index) < 0 || (index) >= (nb_elems)) {\ | |
3518 | av_log(NULL, AV_LOG_ERROR, "Invalid %s index %d while processing metadata maps\n",\ | |
3519 | (desc), (index));\ | |
3520 | exit_program(1);\ | |
3521 | } | |
3522 | ||
847529f8 | 3523 | int in_file_index = o->meta_data_maps[i][1].file; |
e6e6060c AK |
3524 | if (in_file_index < 0) |
3525 | continue; | |
3526 | METADATA_CHECK_INDEX(in_file_index, nb_input_files, "input file") | |
3527 | ||
3528 | files[0] = oc; | |
3529 | files[1] = input_files[in_file_index].ctx; | |
3530 | ||
3531 | for (j = 0; j < 2; j++) { | |
847529f8 | 3532 | MetadataMap *map = &o->meta_data_maps[i][j]; |
e6e6060c AK |
3533 | |
3534 | switch (map->type) { | |
3535 | case 'g': | |
3536 | meta[j] = &files[j]->metadata; | |
3537 | break; | |
3538 | case 's': | |
3539 | METADATA_CHECK_INDEX(map->index, files[j]->nb_streams, "stream") | |
3540 | meta[j] = &files[j]->streams[map->index]->metadata; | |
3541 | break; | |
3542 | case 'c': | |
3543 | METADATA_CHECK_INDEX(map->index, files[j]->nb_chapters, "chapter") | |
3544 | meta[j] = &files[j]->chapters[map->index]->metadata; | |
3545 | break; | |
3546 | case 'p': | |
3547 | METADATA_CHECK_INDEX(map->index, files[j]->nb_programs, "program") | |
3548 | meta[j] = &files[j]->programs[map->index]->metadata; | |
3549 | break; | |
3550 | } | |
3551 | } | |
3552 | ||
3553 | av_dict_copy(meta[0], *meta[1], AV_DICT_DONT_OVERWRITE); | |
3554 | } | |
3555 | ||
3556 | /* copy global metadata by default */ | |
847529f8 | 3557 | if (!o->metadata_global_manual && nb_input_files) |
e6e6060c AK |
3558 | av_dict_copy(&oc->metadata, input_files[0].ctx->metadata, |
3559 | AV_DICT_DONT_OVERWRITE); | |
847529f8 | 3560 | if (!o->metadata_streams_manual) |
4288e031 AK |
3561 | for (i = output_files[nb_output_files - 1].ost_index; i < nb_output_streams; i++) { |
3562 | InputStream *ist = &input_streams[output_streams[i].source_index]; | |
3563 | av_dict_copy(&output_streams[i].st->metadata, ist->st->metadata, AV_DICT_DONT_OVERWRITE); | |
e6e6060c AK |
3564 | } |
3565 | ||
039267f1 AK |
3566 | /* process manually set metadata */ |
3567 | for (i = 0; i < o->nb_metadata; i++) { | |
3568 | AVDictionary **m; | |
3569 | char type, *val; | |
3570 | int index = 0; | |
3571 | ||
3572 | val = strchr(o->metadata[i].u.str, '='); | |
3573 | if (!val) { | |
3574 | av_log(NULL, AV_LOG_ERROR, "No '=' character in metadata string %s.\n", | |
3575 | o->metadata[i].u.str); | |
3576 | exit_program(1); | |
3577 | } | |
3578 | *val++ = 0; | |
3579 | ||
3580 | parse_meta_type(o->metadata[i].specifier, &type, &index); | |
3581 | switch (type) { | |
3582 | case 'g': | |
3583 | m = &oc->metadata; | |
3584 | break; | |
3585 | case 's': | |
3586 | if (index < 0 || index >= oc->nb_streams) { | |
3587 | av_log(NULL, AV_LOG_ERROR, "Invalid stream index %d in metadata specifier.\n", index); | |
3588 | exit_program(1); | |
3589 | } | |
3590 | m = &oc->streams[i]->metadata; | |
3591 | break; | |
3592 | case 'c': | |
3593 | if (index < 0 || index >= oc->nb_chapters) { | |
3594 | av_log(NULL, AV_LOG_ERROR, "Invalid chapter index %d in metadata specifier.\n", index); | |
3595 | exit_program(1); | |
3596 | } | |
3597 | m = &oc->chapters[i]->metadata; | |
3598 | break; | |
3599 | default: | |
3600 | av_log(NULL, AV_LOG_ERROR, "Invalid metadata specifier %s.\n", o->metadata[i].specifier); | |
3601 | exit_program(1); | |
3602 | } | |
3603 | ||
3604 | av_dict_set(m, o->metadata[i].u.str, *val ? val : NULL, 0); | |
3605 | } | |
3606 | ||
6291d7e4 AK |
3607 | frame_rate = (AVRational){0, 0}; |
3608 | frame_width = 0; | |
3609 | frame_height = 0; | |
6291d7e4 | 3610 | |
e922bbfa AK |
3611 | av_freep(&streamid_map); |
3612 | nb_streamid_map = 0; | |
e6e6060c | 3613 | |
575ec4e1 | 3614 | reset_options(o); |
6291d7e4 AK |
3615 | } |
3616 | ||
3617 | /* same option as mencoder */ | |
3618 | static int opt_pass(const char *opt, const char *arg) | |
3619 | { | |
3620 | do_pass = parse_number_or_die(opt, arg, OPT_INT, 1, 2); | |
3621 | return 0; | |
3622 | } | |
3623 | ||
3624 | static int64_t getutime(void) | |
3625 | { | |
3626 | #if HAVE_GETRUSAGE | |
3627 | struct rusage rusage; | |
3628 | ||
3629 | getrusage(RUSAGE_SELF, &rusage); | |
3630 | return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec; | |
3631 | #elif HAVE_GETPROCESSTIMES | |
3632 | HANDLE proc; | |
3633 | FILETIME c, e, k, u; | |
3634 | proc = GetCurrentProcess(); | |
3635 | GetProcessTimes(proc, &c, &e, &k, &u); | |
3636 | return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10; | |
3637 | #else | |
3638 | return av_gettime(); | |
3639 | #endif | |
3640 | } | |
3641 | ||
3642 | static int64_t getmaxrss(void) | |
3643 | { | |
3644 | #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS | |
3645 | struct rusage rusage; | |
3646 | getrusage(RUSAGE_SELF, &rusage); | |
3647 | return (int64_t)rusage.ru_maxrss * 1024; | |
3648 | #elif HAVE_GETPROCESSMEMORYINFO | |
3649 | HANDLE proc; | |
3650 | PROCESS_MEMORY_COUNTERS memcounters; | |
3651 | proc = GetCurrentProcess(); | |
3652 | memcounters.cb = sizeof(memcounters); | |
3653 | GetProcessMemoryInfo(proc, &memcounters, sizeof(memcounters)); | |
3654 | return memcounters.PeakPagefileUsage; | |
3655 | #else | |
3656 | return 0; | |
3657 | #endif | |
3658 | } | |
3659 | ||
3660 | static void parse_matrix_coeffs(uint16_t *dest, const char *str) | |
3661 | { | |
3662 | int i; | |
3663 | const char *p = str; | |
3664 | for(i = 0;; i++) { | |
3665 | dest[i] = atoi(p); | |
3666 | if(i == 63) | |
3667 | break; | |
3668 | p = strchr(p, ','); | |
3669 | if(!p) { | |
3670 | fprintf(stderr, "Syntax error in matrix \"%s\" at coeff %d\n", str, i); | |
3671 | exit_program(1); | |
3672 | } | |
3673 | p++; | |
3674 | } | |
3675 | } | |
3676 | ||
3677 | static void opt_inter_matrix(const char *arg) | |
3678 | { | |
3679 | inter_matrix = av_mallocz(sizeof(uint16_t) * 64); | |
3680 | parse_matrix_coeffs(inter_matrix, arg); | |
3681 | } | |
3682 | ||
3683 | static void opt_intra_matrix(const char *arg) | |
3684 | { | |
3685 | intra_matrix = av_mallocz(sizeof(uint16_t) * 64); | |
3686 | parse_matrix_coeffs(intra_matrix, arg); | |
3687 | } | |
3688 | ||
77d9c454 AK |
3689 | static int opt_audio_qscale(OptionsContext *o, const char *opt, const char *arg) |
3690 | { | |
3691 | return parse_option(o, "q:a", arg, options); | |
3692 | } | |
3693 | ||
6291d7e4 AK |
3694 | static void show_usage(void) |
3695 | { | |
3696 | printf("Hyper fast Audio and Video encoder\n"); | |
3697 | printf("usage: %s [options] [[infile options] -i infile]... {[outfile options] outfile}...\n", program_name); | |
3698 | printf("\n"); | |
3699 | } | |
3700 | ||
3701 | static void show_help(void) | |
3702 | { | |
3703 | AVCodec *c; | |
3704 | AVOutputFormat *oformat = NULL; | |
3705 | AVInputFormat *iformat = NULL; | |
20c21f8b | 3706 | const AVClass *class; |
6291d7e4 AK |
3707 | |
3708 | av_log_set_callback(log_callback_help); | |
3709 | show_usage(); | |
3710 | show_help_options(options, "Main options:\n", | |
3711 | OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_SUBTITLE | OPT_GRAB, 0); | |
3712 | show_help_options(options, "\nAdvanced options:\n", | |
3713 | OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_SUBTITLE | OPT_GRAB, | |
3714 | OPT_EXPERT); | |
3715 | show_help_options(options, "\nVideo options:\n", | |
3716 | OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB, | |
3717 | OPT_VIDEO); | |
3718 | show_help_options(options, "\nAdvanced Video options:\n", | |
3719 | OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB, | |
3720 | OPT_VIDEO | OPT_EXPERT); | |
3721 | show_help_options(options, "\nAudio options:\n", | |
3722 | OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB, | |
3723 | OPT_AUDIO); | |
3724 | show_help_options(options, "\nAdvanced Audio options:\n", | |
3725 | OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB, | |
3726 | OPT_AUDIO | OPT_EXPERT); | |
3727 | show_help_options(options, "\nSubtitle options:\n", | |
3728 | OPT_SUBTITLE | OPT_GRAB, | |
3729 | OPT_SUBTITLE); | |
3730 | show_help_options(options, "\nAudio/Video grab options:\n", | |
3731 | OPT_GRAB, | |
3732 | OPT_GRAB); | |
3733 | printf("\n"); | |
20c21f8b AK |
3734 | class = avcodec_get_class(); |
3735 | av_opt_show2(&class, NULL, AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM, 0); | |
6291d7e4 AK |
3736 | printf("\n"); |
3737 | ||
3738 | /* individual codec options */ | |
3739 | c = NULL; | |
3740 | while ((c = av_codec_next(c))) { | |
3741 | if (c->priv_class) { | |
3742 | av_opt_show2(&c->priv_class, NULL, AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM, 0); | |
3743 | printf("\n"); | |
3744 | } | |
3745 | } | |
3746 | ||
20c21f8b AK |
3747 | class = avformat_get_class(); |
3748 | av_opt_show2(&class, NULL, AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM, 0); | |
6291d7e4 AK |
3749 | printf("\n"); |
3750 | ||
3751 | /* individual muxer options */ | |
3752 | while ((oformat = av_oformat_next(oformat))) { | |
3753 | if (oformat->priv_class) { | |
3754 | av_opt_show2(&oformat->priv_class, NULL, AV_OPT_FLAG_ENCODING_PARAM, 0); | |
3755 | printf("\n"); | |
3756 | } | |
3757 | } | |
3758 | ||
3759 | /* individual demuxer options */ | |
3760 | while ((iformat = av_iformat_next(iformat))) { | |
3761 | if (iformat->priv_class) { | |
3762 | av_opt_show2(&iformat->priv_class, NULL, AV_OPT_FLAG_DECODING_PARAM, 0); | |
3763 | printf("\n"); | |
3764 | } | |
3765 | } | |
3766 | ||
20c21f8b AK |
3767 | class = sws_get_class(); |
3768 | av_opt_show2(&class, NULL, AV_OPT_FLAG_ENCODING_PARAM|AV_OPT_FLAG_DECODING_PARAM, 0); | |
6291d7e4 AK |
3769 | } |
3770 | ||
7041bb3b | 3771 | static int opt_target(OptionsContext *o, const char *opt, const char *arg) |
6291d7e4 AK |
3772 | { |
3773 | enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN; | |
3774 | static const char *const frame_rates[] = {"25", "30000/1001", "24000/1001"}; | |
3775 | ||
3776 | if(!strncmp(arg, "pal-", 4)) { | |
3777 | norm = PAL; | |
3778 | arg += 4; | |
3779 | } else if(!strncmp(arg, "ntsc-", 5)) { | |
3780 | norm = NTSC; | |
3781 | arg += 5; | |
3782 | } else if(!strncmp(arg, "film-", 5)) { | |
3783 | norm = FILM; | |
3784 | arg += 5; | |
3785 | } else { | |
3786 | int fr; | |
3787 | /* Calculate FR via float to avoid int overflow */ | |
3788 | fr = (int)(frame_rate.num * 1000.0 / frame_rate.den); | |
3789 | if(fr == 25000) { | |
3790 | norm = PAL; | |
3791 | } else if((fr == 29970) || (fr == 23976)) { | |
3792 | norm = NTSC; | |
3793 | } else { | |
3794 | /* Try to determine PAL/NTSC by peeking in the input files */ | |
3795 | if(nb_input_files) { | |
3796 | int i, j; | |
3797 | for (j = 0; j < nb_input_files; j++) { | |
ed5b1326 | 3798 | for (i = 0; i < input_files[j].nb_streams; i++) { |
6291d7e4 AK |
3799 | AVCodecContext *c = input_files[j].ctx->streams[i]->codec; |
3800 | if(c->codec_type != AVMEDIA_TYPE_VIDEO) | |
3801 | continue; | |
3802 | fr = c->time_base.den * 1000 / c->time_base.num; | |
3803 | if(fr == 25000) { | |
3804 | norm = PAL; | |
3805 | break; | |
3806 | } else if((fr == 29970) || (fr == 23976)) { | |
3807 | norm = NTSC; | |
3808 | break; | |
3809 | } | |
3810 | } | |
3811 | if(norm != UNKNOWN) | |
3812 | break; | |
3813 | } | |
3814 | } | |
3815 | } | |
3816 | if(verbose > 0 && norm != UNKNOWN) | |
3817 | fprintf(stderr, "Assuming %s for target.\n", norm == PAL ? "PAL" : "NTSC"); | |
3818 | } | |
3819 | ||
3820 | if(norm == UNKNOWN) { | |
3821 | fprintf(stderr, "Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n"); | |
3822 | fprintf(stderr, "Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n"); | |
3823 | fprintf(stderr, "or set a framerate with \"-r xxx\".\n"); | |
3824 | exit_program(1); | |
3825 | } | |
3826 | ||
3827 | if(!strcmp(arg, "vcd")) { | |
35e6f8c1 AK |
3828 | opt_video_codec(o, "c:v", "mpeg1video"); |
3829 | opt_audio_codec(o, "c:a", "mp2"); | |
7041bb3b |