ffmpeg / ffmpeg.c @ dbe94539
History | View | Annotate | Download (161 KB)
1 | 85f07f22 | Fabrice Bellard | /*
|
---|---|---|---|
2 | 89b503b5 | Diego Biurrun | * ffmpeg main
|
3 | 01310af2 | Fabrice Bellard | * Copyright (c) 2000-2003 Fabrice Bellard
|
4 | 85f07f22 | Fabrice Bellard | *
|
5 | 2912e87a | Mans Rullgard | * This file is part of Libav.
|
6 | b78e7197 | Diego Biurrun | *
|
7 | 2912e87a | Mans Rullgard | * Libav is free software; you can redistribute it and/or
|
8 | bf5af568 | Fabrice Bellard | * modify it under the terms of the GNU Lesser General Public
|
9 | * License as published by the Free Software Foundation; either
|
||
10 | b78e7197 | Diego Biurrun | * version 2.1 of the License, or (at your option) any later version.
|
11 | 85f07f22 | Fabrice Bellard | *
|
12 | 2912e87a | Mans Rullgard | * Libav is distributed in the hope that it will be useful,
|
13 | 85f07f22 | Fabrice Bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 | bf5af568 | Fabrice Bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15 | * Lesser General Public License for more details.
|
||
16 | 85f07f22 | Fabrice Bellard | *
|
17 | bf5af568 | Fabrice Bellard | * You should have received a copy of the GNU Lesser General Public
|
18 | 2912e87a | Mans Rullgard | * License along with Libav; if not, write to the Free Software
|
19 | 5509bffa | Diego Biurrun | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
20 | 85f07f22 | Fabrice Bellard | */
|
21 | 364a9607 | Diego Biurrun | |
22 | 0f4e8165 | Ronald S. Bultje | #include "config.h" |
23 | #include <ctype.h> |
||
24 | #include <string.h> |
||
25 | #include <math.h> |
||
26 | #include <stdlib.h> |
||
27 | #include <errno.h> |
||
28 | d86b83f8 | Ramiro Polla | #include <signal.h> |
29 | 22f7a060 | Michael Niedermayer | #include <limits.h> |
30 | 7246177d | Aurelien Jacobs | #include <unistd.h> |
31 | 245976da | Diego Biurrun | #include "libavformat/avformat.h" |
32 | #include "libavdevice/avdevice.h" |
||
33 | #include "libswscale/swscale.h" |
||
34 | 41d0eb1c | Anton Khirnov | #include "libavutil/opt.h" |
35 | ce1ee094 | Peter Ross | #include "libavcodec/audioconvert.h" |
36 | 737eb597 | Reinhard Tartler | #include "libavutil/audioconvert.h" |
37 | #include "libavutil/parseutils.h" |
||
38 | #include "libavutil/samplefmt.h" |
||
39 | 2b4abbd6 | Stefano Sabatini | #include "libavutil/colorspace.h" |
40 | 245976da | Diego Biurrun | #include "libavutil/fifo.h" |
41 | 2839dc97 | Stefano Sabatini | #include "libavutil/intreadwrite.h" |
42 | 718c7b18 | Stefano Sabatini | #include "libavutil/pixdesc.h" |
43 | 245976da | Diego Biurrun | #include "libavutil/avstring.h" |
44 | 335ee1aa | Måns Rullgård | #include "libavutil/libm.h" |
45 | 245976da | Diego Biurrun | #include "libavformat/os_support.h" |
46 | daf8e955 | Fabrice Bellard | |
47 | 46847a33 | Michael Niedermayer | #if CONFIG_AVFILTER
|
48 | # include "libavfilter/avfilter.h" |
||
49 | # include "libavfilter/avfiltergraph.h" |
||
50 | # include "libavfilter/vsrc_buffer.h" |
||
51 | #endif
|
||
52 | |||
53 | b250f9c6 | Aurelien Jacobs | #if HAVE_SYS_RESOURCE_H
|
54 | 0a1b29de | Dave Yeo | #include <sys/types.h> |
55 | fc5607f8 | Reimar Döffinger | #include <sys/time.h> |
56 | b091aa44 | Ramiro Polla | #include <sys/resource.h> |
57 | b250f9c6 | Aurelien Jacobs | #elif HAVE_GETPROCESSTIMES
|
58 | 7495c306 | Ramiro Polla | #include <windows.h> |
59 | #endif
|
||
60 | fc5607f8 | Reimar Döffinger | #if HAVE_GETPROCESSMEMORYINFO
|
61 | #include <windows.h> |
||
62 | #include <psapi.h> |
||
63 | #endif
|
||
64 | 7495c306 | Ramiro Polla | |
65 | b250f9c6 | Aurelien Jacobs | #if HAVE_SYS_SELECT_H
|
66 | fb1d2d7b | Baptiste Coudurier | #include <sys/select.h> |
67 | #endif
|
||
68 | |||
69 | bf5af568 | Fabrice Bellard | #include <time.h> |
70 | 85f07f22 | Fabrice Bellard | |
71 | 01310af2 | Fabrice Bellard | #include "cmdutils.h" |
72 | |||
73 | b64b4134 | Måns Rullgård | #include "libavutil/avassert.h" |
74 | 2b18dcd0 | Michael Niedermayer | |
75 | 89b503b5 | Diego Biurrun | const char program_name[] = "ffmpeg"; |
76 | ea9c581f | Stefano Sabatini | const int program_birth_year = 2000; |
77 | 86074ed1 | Stefano Sabatini | |
78 | 85f07f22 | Fabrice Bellard | /* select an input stream for an output stream */
|
79 | typedef struct AVStreamMap { |
||
80 | int file_index;
|
||
81 | int stream_index;
|
||
82 | b4a3389e | Wolfram Gloger | int sync_file_index;
|
83 | int sync_stream_index;
|
||
84 | 85f07f22 | Fabrice Bellard | } AVStreamMap; |
85 | |||
86 | 3f07e8db | Michael Niedermayer | /**
|
87 | * select an input file for an output file
|
||
88 | */
|
||
89 | 0a38bafd | Patrice Bensoussan | typedef struct AVMetaDataMap { |
90 | 1829e195 | Anton Khirnov | int file; //< file index |
91 | char type; //< type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram |
||
92 | int index; //< stream/chapter/program number |
||
93 | 0a38bafd | Patrice Bensoussan | } AVMetaDataMap; |
94 | |||
95 | 91e96eba | Anton Khirnov | typedef struct AVChapterMap { |
96 | int in_file;
|
||
97 | int out_file;
|
||
98 | } AVChapterMap; |
||
99 | |||
100 | 580a6c57 | Diego Pettenò | static const OptionDef options[]; |
101 | 85f07f22 | Fabrice Bellard | |
102 | 60402344 | Jai Menon | #define MAX_FILES 100 |
103 | 84fd51e5 | Aurelien Jacobs | #define MAX_STREAMS 1024 /* arbitrary sanity check value */ |
104 | 85f07f22 | Fabrice Bellard | |
105 | 3ee53dab | Anton Khirnov | #define FFM_PACKET_SIZE 4096 //XXX a duplicate of the line in ffm.h |
106 | |||
107 | ef6fc647 | Stefano Sabatini | static const char *last_asked_format = NULL; |
108 | 85f07f22 | Fabrice Bellard | static AVFormatContext *input_files[MAX_FILES];
|
109 | a6a92a9a | Wolfram Gloger | static int64_t input_files_ts_offset[MAX_FILES];
|
110 | 2c6958aa | Aurelien Jacobs | static double *input_files_ts_scale[MAX_FILES] = {NULL}; |
111 | 311e223f | Aurelien Jacobs | static AVCodec **input_codecs = NULL; |
112 | 85f07f22 | Fabrice Bellard | static int nb_input_files = 0; |
113 | 311e223f | Aurelien Jacobs | static int nb_input_codecs = 0; |
114 | 2c6958aa | Aurelien Jacobs | static int nb_input_files_ts_scale[MAX_FILES] = {0}; |
115 | 85f07f22 | Fabrice Bellard | |
116 | static AVFormatContext *output_files[MAX_FILES];
|
||
117 | 0a6d97b3 | Aurelien Jacobs | static AVCodec **output_codecs = NULL; |
118 | 85f07f22 | Fabrice Bellard | static int nb_output_files = 0; |
119 | 0a6d97b3 | Aurelien Jacobs | static int nb_output_codecs = 0; |
120 | 85f07f22 | Fabrice Bellard | |
121 | 3a8e8824 | Aurelien Jacobs | static AVStreamMap *stream_maps = NULL; |
122 | 85f07f22 | Fabrice Bellard | static int nb_stream_maps; |
123 | |||
124 | 1829e195 | Anton Khirnov | /* first item specifies output metadata, second is input */
|
125 | static AVMetaDataMap (*meta_data_maps)[2] = NULL; |
||
126 | 0a38bafd | Patrice Bensoussan | static int nb_meta_data_maps; |
127 | 477b1aea | Anton Khirnov | static int metadata_global_autocopy = 1; |
128 | d0abe80a | Anton Khirnov | static int metadata_streams_autocopy = 1; |
129 | static int metadata_chapters_autocopy = 1; |
||
130 | 0a38bafd | Patrice Bensoussan | |
131 | 91e96eba | Anton Khirnov | static AVChapterMap *chapter_maps = NULL; |
132 | static int nb_chapter_maps; |
||
133 | |||
134 | 006e8108 | Mike Scheutzow | /* indexed by output file stream index */
|
135 | e640f261 | Aurelien Jacobs | static int *streamid_map = NULL; |
136 | static int nb_streamid_map = 0; |
||
137 | 006e8108 | Mike Scheutzow | |
138 | 55cf1959 | Michael Niedermayer | static int frame_width = 0; |
139 | static int frame_height = 0; |
||
140 | 880e8ba7 | Roman Shaposhnik | static float frame_aspect_ratio = 0; |
141 | 644a9262 | Michael Niedermayer | static enum PixelFormat frame_pix_fmt = PIX_FMT_NONE; |
142 | 5d6e4c16 | Stefano Sabatini | static enum AVSampleFormat audio_sample_fmt = AV_SAMPLE_FMT_NONE; |
143 | cf7fc795 | Fabrice Bellard | static int max_frames[4] = {INT_MAX, INT_MAX, INT_MAX, INT_MAX}; |
144 | 89129c6b | Diego Biurrun | static AVRational frame_rate;
|
145 | 158c7f05 | Michael Niedermayer | static float video_qscale = 0; |
146 | 84f608f4 | Vidar Madsen | static uint16_t *intra_matrix = NULL; |
147 | static uint16_t *inter_matrix = NULL; |
||
148 | 464a631c | Morten Hustveit | static const char *video_rc_override_string=NULL; |
149 | 85f07f22 | Fabrice Bellard | static int video_disable = 0; |
150 | f3356e9c | Michael Niedermayer | static int video_discard = 0; |
151 | 4a897224 | Nicolas George | static char *video_codec_name = NULL; |
152 | 83a36b2e | Stefano Sabatini | static unsigned int video_codec_tag = 0; |
153 | 0fc2c0f6 | Matthieu Crapet | static char *video_language = NULL; |
154 | 85f07f22 | Fabrice Bellard | static int same_quality = 0; |
155 | cfcf0ffd | Fabrice Bellard | static int do_deinterlace = 0; |
156 | bb198e19 | Michael Niedermayer | static int top_field_first = -1; |
157 | f4f3223f | Michael Niedermayer | static int me_threshold = 0; |
158 | 1a11cbcc | Michael Niedermayer | static int intra_dc_precision = 8; |
159 | 5894e1bb | Víctor Paesa | static int loop_input = 0; |
160 | 8108551a | Todd Kirby | static int loop_output = AVFMT_NOOUTPUTLOOP; |
161 | 0888fd22 | Michael Niedermayer | static int qp_hist = 0; |
162 | 46847a33 | Michael Niedermayer | #if CONFIG_AVFILTER
|
163 | static char *vfilters = NULL; |
||
164 | a6d1bd05 | Diego Elio Pettenò | static AVFilterGraph *graph = NULL; |
165 | 46847a33 | Michael Niedermayer | #endif
|
166 | 85f07f22 | Fabrice Bellard | |
167 | static int intra_only = 0; |
||
168 | static int audio_sample_rate = 44100; |
||
169 | 13367a46 | Benjamin Larsson | static int64_t channel_layout = 0; |
170 | c57c770d | Justin Ruggles | #define QSCALE_NONE -99999 |
171 | static float audio_qscale = QSCALE_NONE; |
||
172 | 85f07f22 | Fabrice Bellard | static int audio_disable = 0; |
173 | static int audio_channels = 1; |
||
174 | 4a897224 | Nicolas George | static char *audio_codec_name = NULL; |
175 | 83a36b2e | Stefano Sabatini | static unsigned int audio_codec_tag = 0; |
176 | cf7fc795 | Fabrice Bellard | static char *audio_language = NULL; |
177 | |||
178 | 11bf3847 | Aurelien Jacobs | static int subtitle_disable = 0; |
179 | 4a897224 | Nicolas George | static char *subtitle_codec_name = NULL; |
180 | cf7fc795 | Fabrice Bellard | static char *subtitle_language = NULL; |
181 | 83a36b2e | Stefano Sabatini | static unsigned int subtitle_codec_tag = 0; |
182 | 85f07f22 | Fabrice Bellard | |
183 | e3b540b4 | Luca Barbato | static int data_disable = 0; |
184 | static char *data_codec_name = NULL; |
||
185 | static unsigned int data_codec_tag = 0; |
||
186 | |||
187 | 17c88cb0 | Michael Niedermayer | static float mux_preload= 0.5; |
188 | static float mux_max_delay= 0.7; |
||
189 | 2db3c638 | Michael Niedermayer | |
190 | fc7ad2af | Stefano Sabatini | static int64_t recording_time = INT64_MAX;
|
191 | 8831db5c | Michael Niedermayer | static int64_t start_time = 0; |
192 | 25d34458 | Stefano Sabatini | static int64_t recording_timestamp = 0; |
193 | a6a92a9a | Wolfram Gloger | static int64_t input_ts_offset = 0; |
194 | 85f07f22 | Fabrice Bellard | static int file_overwrite = 0; |
195 | 4bf65e2a | Stefano Sabatini | static AVMetadata *metadata;
|
196 | 5727b222 | Fabrice Bellard | static int do_benchmark = 0; |
197 | a0663ba4 | Fabrice Bellard | static int do_hex_dump = 0; |
198 | 254abc2e | Fabrice Bellard | static int do_pkt_dump = 0; |
199 | 43f1708f | Juanjo | static int do_psnr = 0; |
200 | 5abdb4b1 | Fabrice Bellard | static int do_pass = 0; |
201 | ad16627f | Stefano Sabatini | static char *pass_logfilename_prefix = NULL; |
202 | 1629626f | Fabrice Bellard | static int audio_stream_copy = 0; |
203 | static int video_stream_copy = 0; |
||
204 | cf7fc795 | Fabrice Bellard | static int subtitle_stream_copy = 0; |
205 | e3b540b4 | Luca Barbato | static int data_stream_copy = 0; |
206 | 8858816d | Michael Niedermayer | static int video_sync_method= -1; |
207 | 986ebcdb | Michael Niedermayer | static int audio_sync_method= 0; |
208 | d4d226a8 | Michael Niedermayer | static float audio_drift_threshold= 0.1; |
209 | 72bd8100 | Michael Niedermayer | static int copy_ts= 0; |
210 | 0f27e6b4 | Baptiste Coudurier | static int copy_tb; |
211 | 76bdac6d | Stefano Sabatini | static int opt_shortest = 0; |
212 | 90ad92b3 | Michael Niedermayer | static int video_global_header = 0; |
213 | b60d1379 | Stefano Sabatini | static char *vstats_filename; |
214 | 032aa7df | Stefano Sabatini | static FILE *vstats_file;
|
215 | 50e143c4 | Nico Sabbi | static int opt_programid = 0; |
216 | 50e3477f | Wolfram Gloger | static int copy_initial_nonkeyframes = 0; |
217 | 5abdb4b1 | Fabrice Bellard | |
218 | bdfcbbed | Max Krasnyansky | static int rate_emu = 0; |
219 | |||
220 | a5df11ab | Fabrice Bellard | static int video_channel = 0; |
221 | df0cecdd | Luca Abeni | static char *video_standard; |
222 | 79a7c268 | Fabrice Bellard | |
223 | a9aa3467 | Michael Niedermayer | static int audio_volume = 256; |
224 | 8aa3ee32 | Max Krasnyansky | |
225 | f2abc559 | Baptiste Coudurier | static int exit_on_error = 0; |
226 | d9a916e2 | Charles Yates | static int using_stdin = 0; |
227 | f068206e | Bill Eldridge | static int verbose = 1; |
228 | 9c3d33d6 | Michael Niedermayer | static int thread_count= 1; |
229 | 1008ceb3 | Michael Niedermayer | static int64_t video_size = 0; |
230 | static int64_t audio_size = 0; |
||
231 | static int64_t extra_size = 0; |
||
232 | a6a92a9a | Wolfram Gloger | static int nb_frames_dup = 0; |
233 | static int nb_frames_drop = 0; |
||
234 | 6e454c38 | Luca Abeni | static int input_sync; |
235 | 76bdac6d | Stefano Sabatini | static uint64_t limit_filesize = 0; |
236 | d2845b75 | Stefano Sabatini | static int force_fps = 0; |
237 | 4ad08021 | Nicolas George | static char *forced_key_frames = NULL; |
238 | d9a916e2 | Charles Yates | |
239 | a8482aab | Michael Niedermayer | static float dts_delta_threshold = 10; |
240 | 5b6d5596 | Michael Niedermayer | |
241 | 29cc1c23 | Benoit Fouet | static int64_t timer_start;
|
242 | 8bbf6db9 | Michael Niedermayer | |
243 | 3321cb3f | Baptiste Coudurier | static uint8_t *audio_buf;
|
244 | static uint8_t *audio_out;
|
||
245 | a6d1bd05 | Diego Elio Pettenò | static unsigned int allocated_audio_out_size, allocated_audio_buf_size; |
246 | 3321cb3f | Baptiste Coudurier | |
247 | static short *samples; |
||
248 | |||
249 | 748c2fca | Michael Niedermayer | static AVBitStreamFilterContext *video_bitstream_filters=NULL; |
250 | static AVBitStreamFilterContext *audio_bitstream_filters=NULL; |
||
251 | e1cc8339 | Reimar Döffinger | static AVBitStreamFilterContext *subtitle_bitstream_filters=NULL; |
252 | 5b6d5596 | Michael Niedermayer | |
253 | ad16627f | Stefano Sabatini | #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass" |
254 | 85f07f22 | Fabrice Bellard | |
255 | b4a3389e | Wolfram Gloger | struct AVInputStream;
|
256 | |||
257 | 85f07f22 | Fabrice Bellard | typedef struct AVOutputStream { |
258 | int file_index; /* file index */ |
||
259 | int index; /* stream index in the output file */ |
||
260 | int source_index; /* AVInputStream index */ |
||
261 | AVStream *st; /* stream in the output file */
|
||
262 | ec5517d5 | Fabrice Bellard | int encoding_needed; /* true if encoding needed for this stream */ |
263 | int frame_number;
|
||
264 | /* input pts and corresponding output pts
|
||
265 | for A/V sync */
|
||
266 | b4a3389e | Wolfram Gloger | //double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
|
267 | struct AVInputStream *sync_ist; /* input stream to sync against */ |
||
268 | e928649b | Michael Niedermayer | int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ //FIXME look at frame_number |
269 | 0b6d358a | Nicolas George | AVBitStreamFilterContext *bitstream_filters; |
270 | 85f07f22 | Fabrice Bellard | /* video only */
|
271 | 07d0cdfc | Luca Abeni | int video_resample;
|
272 | a4d36c11 | Michael Niedermayer | AVFrame pict_tmp; /* temporary image for resampling */
|
273 | 18a54b04 | Luca Abeni | struct SwsContext *img_resample_ctx; /* for image resampling */ |
274 | int resample_height;
|
||
275 | 352666c1 | Eric Buehl | int resample_width;
|
276 | 01a3c821 | Jason Garrett-Glaser | int resample_pix_fmt;
|
277 | 34b10a57 | Dieter | |
278 | 4ad08021 | Nicolas George | /* forced key frames */
|
279 | int64_t *forced_kf_pts; |
||
280 | int forced_kf_count;
|
||
281 | int forced_kf_index;
|
||
282 | |||
283 | 85f07f22 | Fabrice Bellard | /* audio only */
|
284 | int audio_resample;
|
||
285 | ReSampleContext *resample; /* for audio resampling */
|
||
286 | 8afab686 | Stefano Sabatini | int resample_sample_fmt;
|
287 | int resample_channels;
|
||
288 | int resample_sample_rate;
|
||
289 | a79db0f7 | Peter Ross | int reformat_pair;
|
290 | AVAudioConvert *reformat_ctx; |
||
291 | 41dd680d | Michael Niedermayer | AVFifoBuffer *fifo; /* for compression: one audio fifo per codec */
|
292 | 5abdb4b1 | Fabrice Bellard | FILE *logfile; |
293 | 1435f2fa | Anton Khirnov | |
294 | int sws_flags;
|
||
295 | 85f07f22 | Fabrice Bellard | } AVOutputStream; |
296 | |||
297 | 9fdf4b58 | Nicolas George | static AVOutputStream **output_streams_for_file[MAX_FILES] = { NULL }; |
298 | static int nb_output_streams_for_file[MAX_FILES] = { 0 }; |
||
299 | |||
300 | 85f07f22 | Fabrice Bellard | typedef struct AVInputStream { |
301 | int file_index;
|
||
302 | int index;
|
||
303 | AVStream *st; |
||
304 | int discard; /* true if stream data should be discarded */ |
||
305 | int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */ |
||
306 | 0c1a9eda | Zdenek Kabelac | int64_t sample_index; /* current sample */
|
307 | bdfcbbed | Max Krasnyansky | |
308 | int64_t start; /* time when read started */
|
||
309 | 254abc2e | Fabrice Bellard | int64_t next_pts; /* synthetic pts for cases where pkt.pts
|
310 | is not defined */
|
||
311 | e7d0374f | Roman Shaposhnik | int64_t pts; /* current pts */
|
312 | 0ff4f0c0 | Alexander Strange | PtsCorrectionContext pts_ctx; |
313 | ff4905a5 | Michael Niedermayer | int is_start; /* is 1 at the start and after a discontinuity */ |
314 | 3ff0daf0 | Michael Niedermayer | int showed_multi_packet_warning;
|
315 | 55a7e946 | Wolfram Gloger | int is_past_recording_time;
|
316 | 46847a33 | Michael Niedermayer | #if CONFIG_AVFILTER
|
317 | dfd57757 | Stefano Sabatini | AVFilterContext *output_video_filter; |
318 | 46847a33 | Michael Niedermayer | AVFilterContext *input_video_filter; |
319 | AVFrame *filter_frame; |
||
320 | int has_filter_frame;
|
||
321 | ecc8dada | S.N. Hemanth Meenakshisundaram | AVFilterBufferRef *picref; |
322 | 46847a33 | Michael Niedermayer | #endif
|
323 | 85f07f22 | Fabrice Bellard | } AVInputStream; |
324 | |||
325 | typedef struct AVInputFile { |
||
326 | int eof_reached; /* true if eof reached */ |
||
327 | int ist_index; /* index of first stream in ist_table */ |
||
328 | int buffer_size; /* current total buffer size */ |
||
329 | 79fdaa4c | Fabrice Bellard | int nb_streams; /* nb streams we are aware of */ |
330 | 85f07f22 | Fabrice Bellard | } AVInputFile; |
331 | |||
332 | 46847a33 | Michael Niedermayer | #if CONFIG_AVFILTER
|
333 | |||
334 | 0420bf09 | Stefano Sabatini | static int configure_video_filters(AVInputStream *ist, AVOutputStream *ost) |
335 | 46847a33 | Michael Niedermayer | { |
336 | a9f3cb93 | Baptiste Coudurier | AVFilterContext *last_filter, *filter; |
337 | 46847a33 | Michael Niedermayer | /** filter graph containing all filters including input & output */
|
338 | AVCodecContext *codec = ost->st->codec; |
||
339 | AVCodecContext *icodec = ist->st->codec; |
||
340 | f7ead94c | Stefano Sabatini | FFSinkContext ffsink_ctx = { .pix_fmt = codec->pix_fmt }; |
341 | 7a11c82f | Michael Niedermayer | AVRational sample_aspect_ratio; |
342 | 46847a33 | Michael Niedermayer | char args[255]; |
343 | 4ddf0d29 | Stefano Sabatini | int ret;
|
344 | 46847a33 | Michael Niedermayer | |
345 | e15aeea6 | Stefano Sabatini | graph = avfilter_graph_alloc(); |
346 | 46847a33 | Michael Niedermayer | |
347 | 7a11c82f | Michael Niedermayer | if (ist->st->sample_aspect_ratio.num){
|
348 | sample_aspect_ratio = ist->st->sample_aspect_ratio; |
||
349 | }else
|
||
350 | sample_aspect_ratio = ist->st->codec->sample_aspect_ratio; |
||
351 | |||
352 | snprintf(args, 255, "%d:%d:%d:%d:%d:%d:%d", ist->st->codec->width, |
||
353 | ist->st->codec->height, ist->st->codec->pix_fmt, 1, AV_TIME_BASE,
|
||
354 | sample_aspect_ratio.num, sample_aspect_ratio.den); |
||
355 | |||
356 | 037be76e | Stefano Sabatini | ret = avfilter_graph_create_filter(&ist->input_video_filter, avfilter_get_by_name("buffer"),
|
357 | "src", args, NULL, graph); |
||
358 | if (ret < 0) |
||
359 | 4ddf0d29 | Stefano Sabatini | return ret;
|
360 | 037be76e | Stefano Sabatini | ret = avfilter_graph_create_filter(&ist->output_video_filter, &ffsink, |
361 | "out", NULL, &ffsink_ctx, graph); |
||
362 | if (ret < 0) |
||
363 | 4ddf0d29 | Stefano Sabatini | return ret;
|
364 | a9f3cb93 | Baptiste Coudurier | last_filter = ist->input_video_filter; |
365 | 46847a33 | Michael Niedermayer | |
366 | 5879ea6d | Stefano Sabatini | if (codec->width != icodec->width || codec->height != icodec->height) {
|
367 | 6e82e7fa | Baptiste Coudurier | snprintf(args, 255, "%d:%d:flags=0x%X", |
368 | 0c22311b | Michael Niedermayer | codec->width, |
369 | codec->height, |
||
370 | 1435f2fa | Anton Khirnov | ost->sws_flags); |
371 | 037be76e | Stefano Sabatini | if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"), |
372 | NULL, args, NULL, graph)) < 0) |
||
373 | 4ddf0d29 | Stefano Sabatini | return ret;
|
374 | if ((ret = avfilter_link(last_filter, 0, filter, 0)) < 0) |
||
375 | return ret;
|
||
376 | a9f3cb93 | Baptiste Coudurier | last_filter = filter; |
377 | 46847a33 | Michael Niedermayer | } |
378 | |||
379 | 1435f2fa | Anton Khirnov | snprintf(args, sizeof(args), "flags=0x%X", ost->sws_flags); |
380 | f96363df | Baptiste Coudurier | graph->scale_sws_opts = av_strdup(args); |
381 | |||
382 | 79b90b25 | Baptiste Coudurier | if (vfilters) {
|
383 | 46847a33 | Michael Niedermayer | AVFilterInOut *outputs = av_malloc(sizeof(AVFilterInOut));
|
384 | AVFilterInOut *inputs = av_malloc(sizeof(AVFilterInOut));
|
||
385 | |||
386 | outputs->name = av_strdup("in");
|
||
387 | 7313132b | Stefano Sabatini | outputs->filter_ctx = last_filter; |
388 | 46847a33 | Michael Niedermayer | outputs->pad_idx = 0;
|
389 | outputs->next = NULL;
|
||
390 | |||
391 | inputs->name = av_strdup("out");
|
||
392 | 7313132b | Stefano Sabatini | inputs->filter_ctx = ist->output_video_filter; |
393 | 46847a33 | Michael Niedermayer | inputs->pad_idx = 0;
|
394 | inputs->next = NULL;
|
||
395 | |||
396 | 4ddf0d29 | Stefano Sabatini | if ((ret = avfilter_graph_parse(graph, vfilters, inputs, outputs, NULL)) < 0) |
397 | return ret;
|
||
398 | 46847a33 | Michael Niedermayer | av_freep(&vfilters); |
399 | } else {
|
||
400 | dfd57757 | Stefano Sabatini | if ((ret = avfilter_link(last_filter, 0, ist->output_video_filter, 0)) < 0) |
401 | 4ddf0d29 | Stefano Sabatini | return ret;
|
402 | 46847a33 | Michael Niedermayer | } |
403 | |||
404 | 2a24df93 | Stefano Sabatini | if ((ret = avfilter_graph_config(graph, NULL)) < 0) |
405 | 4ddf0d29 | Stefano Sabatini | return ret;
|
406 | 46847a33 | Michael Niedermayer | |
407 | dfd57757 | Stefano Sabatini | codec->width = ist->output_video_filter->inputs[0]->w;
|
408 | codec->height = ist->output_video_filter->inputs[0]->h;
|
||
409 | 7a11c82f | Michael Niedermayer | codec->sample_aspect_ratio = ost->st->sample_aspect_ratio = |
410 | ist->output_video_filter->inputs[0]->sample_aspect_ratio;
|
||
411 | 46847a33 | Michael Niedermayer | |
412 | return 0; |
||
413 | } |
||
414 | #endif /* CONFIG_AVFILTER */ |
||
415 | |||
416 | 85f07f22 | Fabrice Bellard | static void term_exit(void) |
417 | { |
||
418 | 6b6bca64 | Michael Niedermayer | av_log(NULL, AV_LOG_QUIET, ""); |
419 | 64f6e357 | Måns Rullgård | } |
420 | 85f07f22 | Fabrice Bellard | |
421 | e9a832e5 | Martin Storsjö | static volatile int received_sigterm = 0; |
422 | 9680a722 | Roumen Petrov | |
423 | static void |
||
424 | sigterm_handler(int sig)
|
||
425 | { |
||
426 | received_sigterm = sig; |
||
427 | term_exit(); |
||
428 | } |
||
429 | |||
430 | 85f07f22 | Fabrice Bellard | static void term_init(void) |
431 | { |
||
432 | 9680a722 | Roumen Petrov | signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
|
433 | signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
|
||
434 | ffcc6e24 | Måns Rullgård | #ifdef SIGXCPU
|
435 | signal(SIGXCPU, sigterm_handler); |
||
436 | #endif
|
||
437 | 85f07f22 | Fabrice Bellard | } |
438 | |||
439 | b51469a0 | Leon van Stuivenberg | static int decode_interrupt_cb(void) |
440 | { |
||
441 | 8fb566fd | Anton Khirnov | return received_sigterm;
|
442 | b51469a0 | Leon van Stuivenberg | } |
443 | |||
444 | 639e4ec8 | Stefano Sabatini | static int ffmpeg_exit(int ret) |
445 | e5295c0d | Ramiro Polla | { |
446 | int i;
|
||
447 | |||
448 | /* close files */
|
||
449 | for(i=0;i<nb_output_files;i++) { |
||
450 | AVFormatContext *s = output_files[i]; |
||
451 | 8767060c | Ramiro Polla | if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
|
452 | 22a3212e | Anton Khirnov | avio_close(s->pb); |
453 | b22dbb29 | Martin Storsjö | avformat_free_context(s); |
454 | 9fdf4b58 | Nicolas George | av_free(output_streams_for_file[i]); |
455 | e5295c0d | Ramiro Polla | } |
456 | 2c6958aa | Aurelien Jacobs | for(i=0;i<nb_input_files;i++) { |
457 | e5295c0d | Ramiro Polla | av_close_input_file(input_files[i]); |
458 | 2c6958aa | Aurelien Jacobs | av_free(input_files_ts_scale[i]); |
459 | } |
||
460 | e5295c0d | Ramiro Polla | |
461 | av_free(intra_matrix); |
||
462 | av_free(inter_matrix); |
||
463 | |||
464 | if (vstats_file)
|
||
465 | fclose(vstats_file); |
||
466 | av_free(vstats_filename); |
||
467 | |||
468 | e640f261 | Aurelien Jacobs | av_free(streamid_map); |
469 | 311e223f | Aurelien Jacobs | av_free(input_codecs); |
470 | 0a6d97b3 | Aurelien Jacobs | av_free(output_codecs); |
471 | 3a8e8824 | Aurelien Jacobs | av_free(stream_maps); |
472 | 63e856df | Anton Khirnov | av_free(meta_data_maps); |
473 | e5295c0d | Ramiro Polla | |
474 | av_free(video_codec_name); |
||
475 | av_free(audio_codec_name); |
||
476 | av_free(subtitle_codec_name); |
||
477 | e3b540b4 | Luca Barbato | av_free(data_codec_name); |
478 | e5295c0d | Ramiro Polla | |
479 | av_free(video_standard); |
||
480 | |||
481 | a5c33faa | Reimar Döffinger | uninit_opts(); |
482 | 3321cb3f | Baptiste Coudurier | av_free(audio_buf); |
483 | av_free(audio_out); |
||
484 | b8919a30 | Michael Niedermayer | allocated_audio_buf_size= allocated_audio_out_size= 0;
|
485 | 3321cb3f | Baptiste Coudurier | av_free(samples); |
486 | 5973490a | Baptiste Coudurier | |
487 | 46847a33 | Michael Niedermayer | #if CONFIG_AVFILTER
|
488 | avfilter_uninit(); |
||
489 | #endif
|
||
490 | |||
491 | e5295c0d | Ramiro Polla | if (received_sigterm) {
|
492 | fprintf(stderr, |
||
493 | "Received signal %d: terminating.\n",
|
||
494 | (int) received_sigterm);
|
||
495 | exit (255);
|
||
496 | } |
||
497 | |||
498 | 296df4e7 | Ramiro Polla | exit(ret); /* not all OS-es handle main() return value */
|
499 | return ret;
|
||
500 | dba249ab | Aurelien Jacobs | } |
501 | |||
502 | /* similar to ff_dynarray_add() and av_fast_realloc() */
|
||
503 | static void *grow_array(void *array, int elem_size, int *size, int new_size) |
||
504 | { |
||
505 | if (new_size >= INT_MAX / elem_size) {
|
||
506 | fprintf(stderr, "Array too big.\n");
|
||
507 | ffmpeg_exit(1);
|
||
508 | } |
||
509 | if (*size < new_size) {
|
||
510 | uint8_t *tmp = av_realloc(array, new_size*elem_size); |
||
511 | if (!tmp) {
|
||
512 | fprintf(stderr, "Could not alloc buffer.\n");
|
||
513 | ffmpeg_exit(1);
|
||
514 | } |
||
515 | memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);
|
||
516 | *size = new_size; |
||
517 | return tmp;
|
||
518 | } |
||
519 | return array;
|
||
520 | e5295c0d | Ramiro Polla | } |
521 | |||
522 | aa1de0d9 | Ronald S. Bultje | static void choose_sample_fmt(AVStream *st, AVCodec *codec) |
523 | { |
||
524 | if(codec && codec->sample_fmts){
|
||
525 | 5d6e4c16 | Stefano Sabatini | const enum AVSampleFormat *p= codec->sample_fmts; |
526 | aa1de0d9 | Ronald S. Bultje | for(; *p!=-1; p++){ |
527 | if(*p == st->codec->sample_fmt)
|
||
528 | break;
|
||
529 | } |
||
530 | fa34a362 | Stefano Sabatini | if (*p == -1) { |
531 | av_log(NULL, AV_LOG_WARNING,
|
||
532 | "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
|
||
533 | av_get_sample_fmt_name(st->codec->sample_fmt), |
||
534 | codec->name, |
||
535 | av_get_sample_fmt_name(codec->sample_fmts[0]));
|
||
536 | aa1de0d9 | Ronald S. Bultje | st->codec->sample_fmt = codec->sample_fmts[0];
|
537 | fa34a362 | Stefano Sabatini | } |
538 | aa1de0d9 | Ronald S. Bultje | } |
539 | } |
||
540 | |||
541 | 10d0f5e0 | Michael Niedermayer | static void choose_sample_rate(AVStream *st, AVCodec *codec) |
542 | { |
||
543 | if(codec && codec->supported_samplerates){
|
||
544 | const int *p= codec->supported_samplerates; |
||
545 | 3c5e1b36 | Baptiste Coudurier | int best=0; |
546 | 10d0f5e0 | Michael Niedermayer | int best_dist=INT_MAX;
|
547 | for(; *p; p++){
|
||
548 | int dist= abs(st->codec->sample_rate - *p);
|
||
549 | if(dist < best_dist){
|
||
550 | best_dist= dist; |
||
551 | best= *p; |
||
552 | } |
||
553 | } |
||
554 | ff866063 | Michael Niedermayer | if(best_dist){
|
555 | av_log(st->codec, AV_LOG_WARNING, "Requested sampling rate unsupported using closest supported (%d)\n", best);
|
||
556 | } |
||
557 | 10d0f5e0 | Michael Niedermayer | st->codec->sample_rate= best; |
558 | } |
||
559 | } |
||
560 | |||
561 | aa1de0d9 | Ronald S. Bultje | static void choose_pixel_fmt(AVStream *st, AVCodec *codec) |
562 | { |
||
563 | if(codec && codec->pix_fmts){
|
||
564 | const enum PixelFormat *p= codec->pix_fmts; |
||
565 | b26847b7 | Michael Niedermayer | if(st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL){
|
566 | if(st->codec->codec_id==CODEC_ID_MJPEG){
|
||
567 | p= (const enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE}; |
||
568 | }else if(st->codec->codec_id==CODEC_ID_LJPEG){ |
||
569 | 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}; |
||
570 | } |
||
571 | } |
||
572 | aa1de0d9 | Ronald S. Bultje | for(; *p!=-1; p++){ |
573 | if(*p == st->codec->pix_fmt)
|
||
574 | break;
|
||
575 | } |
||
576 | b568d6d9 | Stefano Sabatini | if (*p == -1) { |
577 | if(st->codec->pix_fmt != PIX_FMT_NONE)
|
||
578 | av_log(NULL, AV_LOG_WARNING,
|
||
579 | "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
|
||
580 | av_pix_fmt_descriptors[st->codec->pix_fmt].name, |
||
581 | codec->name, |
||
582 | av_pix_fmt_descriptors[codec->pix_fmts[0]].name);
|
||
583 | aa1de0d9 | Ronald S. Bultje | st->codec->pix_fmt = codec->pix_fmts[0];
|
584 | b568d6d9 | Stefano Sabatini | } |
585 | aa1de0d9 | Ronald S. Bultje | } |
586 | } |
||
587 | |||
588 | ec1ca41c | Carl Eugen Hoyos | static AVOutputStream *new_output_stream(AVFormatContext *oc, int file_idx) |
589 | { |
||
590 | int idx = oc->nb_streams - 1; |
||
591 | AVOutputStream *ost; |
||
592 | |||
593 | output_streams_for_file[file_idx] = |
||
594 | grow_array(output_streams_for_file[file_idx], |
||
595 | sizeof(*output_streams_for_file[file_idx]),
|
||
596 | &nb_output_streams_for_file[file_idx], |
||
597 | oc->nb_streams); |
||
598 | ost = output_streams_for_file[file_idx][idx] = |
||
599 | av_mallocz(sizeof(AVOutputStream));
|
||
600 | if (!ost) {
|
||
601 | fprintf(stderr, "Could not alloc output stream\n");
|
||
602 | ffmpeg_exit(1);
|
||
603 | } |
||
604 | ost->file_index = file_idx; |
||
605 | ost->index = idx; |
||
606 | 1435f2fa | Anton Khirnov | |
607 | ost->sws_flags = av_get_int(sws_opts, "sws_flags", NULL); |
||
608 | ec1ca41c | Carl Eugen Hoyos | return ost;
|
609 | } |
||
610 | |||
611 | b29f97d1 | Zdenek Kabelac | static int read_ffserver_streams(AVFormatContext *s, const char *filename) |
612 | 85f07f22 | Fabrice Bellard | { |
613 | 79fdaa4c | Fabrice Bellard | int i, err;
|
614 | 85f07f22 | Fabrice Bellard | AVFormatContext *ic; |
615 | 3438d82d | Baptiste Coudurier | int nopts = 0; |
616 | 85f07f22 | Fabrice Bellard | |
617 | 79fdaa4c | Fabrice Bellard | err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL); |
618 | if (err < 0) |
||
619 | return err;
|
||
620 | 85f07f22 | Fabrice Bellard | /* copy stream format */
|
621 | b67f3d65 | Rocky Cardwell | s->nb_streams = 0;
|
622 | 85f07f22 | Fabrice Bellard | for(i=0;i<ic->nb_streams;i++) { |
623 | AVStream *st; |
||
624 | d9521cb1 | Ronald S. Bultje | AVCodec *codec; |
625 | 1e491e29 | Michael Niedermayer | |
626 | b67f3d65 | Rocky Cardwell | s->nb_streams++; |
627 | |||
628 | f37f8d4c | Alex Beregszaszi | // FIXME: a more elegant solution is needed
|
629 | e1031171 | Alex Beregszaszi | st = av_mallocz(sizeof(AVStream));
|
630 | 85f07f22 | Fabrice Bellard | memcpy(st, ic->streams[i], sizeof(AVStream));
|
631 | f37f8d4c | Alex Beregszaszi | st->codec = avcodec_alloc_context(); |
632 | 7ef61879 | Ramiro Polla | if (!st->codec) {
|
633 | print_error(filename, AVERROR(ENOMEM)); |
||
634 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
635 | 7ef61879 | Ramiro Polla | } |
636 | d9521cb1 | Ronald S. Bultje | avcodec_copy_context(st->codec, ic->streams[i]->codec); |
637 | 85f07f22 | Fabrice Bellard | s->streams[i] = st; |
638 | 837d248d | Baptiste Coudurier | |
639 | d9521cb1 | Ronald S. Bultje | codec = avcodec_find_encoder(st->codec->codec_id); |
640 | if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
|
||
641 | if (audio_stream_copy) {
|
||
642 | st->stream_copy = 1;
|
||
643 | } else
|
||
644 | choose_sample_fmt(st, codec); |
||
645 | } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { |
||
646 | if (video_stream_copy) {
|
||
647 | st->stream_copy = 1;
|
||
648 | } else
|
||
649 | choose_pixel_fmt(st, codec); |
||
650 | } |
||
651 | 837d248d | Baptiste Coudurier | |
652 | 3438d82d | Baptiste Coudurier | if(st->codec->flags & CODEC_FLAG_BITEXACT)
|
653 | nopts = 1;
|
||
654 | b67f3d65 | Rocky Cardwell | |
655 | new_output_stream(s, nb_output_files); |
||
656 | 85f07f22 | Fabrice Bellard | } |
657 | |||
658 | 3438d82d | Baptiste Coudurier | if (!nopts)
|
659 | s->timestamp = av_gettime(); |
||
660 | |||
661 | 85f07f22 | Fabrice Bellard | av_close_input_file(ic); |
662 | return 0; |
||
663 | } |
||
664 | |||
665 | b4a3389e | Wolfram Gloger | static double |
666 | get_sync_ipts(const AVOutputStream *ost)
|
||
667 | { |
||
668 | const AVInputStream *ist = ost->sync_ist;
|
||
669 | fec401f7 | Michael Niedermayer | return (double)(ist->pts - start_time)/AV_TIME_BASE; |
670 | b4a3389e | Wolfram Gloger | } |
671 | |||
672 | 748c2fca | Michael Niedermayer | static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx, AVBitStreamFilterContext *bsfc){ |
673 | 0ac07031 | Michael Niedermayer | int ret;
|
674 | |||
675 | 748c2fca | Michael Niedermayer | while(bsfc){
|
676 | AVPacket new_pkt= *pkt; |
||
677 | int a= av_bitstream_filter_filter(bsfc, avctx, NULL, |
||
678 | &new_pkt.data, &new_pkt.size, |
||
679 | pkt->data, pkt->size, |
||
680 | cc947f04 | Jean-Daniel Dupas | pkt->flags & AV_PKT_FLAG_KEY); |
681 | 7055cdac | Benoit Fouet | if(a>0){ |
682 | 748c2fca | Michael Niedermayer | av_free_packet(pkt); |
683 | new_pkt.destruct= av_destruct_packet; |
||
684 | 7055cdac | Benoit Fouet | } else if(a<0){ |
685 | 1f8e32cd | Diego Biurrun | fprintf(stderr, "%s failed for stream %d, codec %s",
|
686 | bsfc->filter->name, pkt->stream_index, |
||
687 | avctx->codec ? avctx->codec->name : "copy");
|
||
688 | 7055cdac | Benoit Fouet | print_error("", a);
|
689 | f2abc559 | Baptiste Coudurier | if (exit_on_error)
|
690 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
691 | 748c2fca | Michael Niedermayer | } |
692 | *pkt= new_pkt; |
||
693 | |||
694 | bsfc= bsfc->next; |
||
695 | } |
||
696 | |||
697 | 0ac07031 | Michael Niedermayer | ret= av_interleaved_write_frame(s, pkt); |
698 | if(ret < 0){ |
||
699 | print_error("av_interleaved_write_frame()", ret);
|
||
700 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
701 | 0ac07031 | Michael Niedermayer | } |
702 | 748c2fca | Michael Niedermayer | } |
703 | |||
704 | 817b23ff | Fabrice Bellard | #define MAX_AUDIO_PACKET_SIZE (128 * 1024) |
705 | 85f07f22 | Fabrice Bellard | |
706 | 115329f1 | Diego Biurrun | static void do_audio_out(AVFormatContext *s, |
707 | AVOutputStream *ost, |
||
708 | 85f07f22 | Fabrice Bellard | AVInputStream *ist, |
709 | unsigned char *buf, int size) |
||
710 | { |
||
711 | 0c1a9eda | Zdenek Kabelac | uint8_t *buftmp; |
712 | 15bfe412 | Michael Niedermayer | int64_t audio_out_size, audio_buf_size; |
713 | 7a086a85 | Michael Niedermayer | int64_t allocated_for_size= size; |
714 | d66c7abc | Sylvain Corré | |
715 | 8afab686 | Stefano Sabatini | int size_out, frame_bytes, ret, resample_changed;
|
716 | 01f4895c | Michael Niedermayer | AVCodecContext *enc= ost->st->codec; |
717 | 2886f311 | Andreas Öman | AVCodecContext *dec= ist->st->codec; |
718 | ba7d6e79 | Stefano Sabatini | int osize= av_get_bits_per_sample_fmt(enc->sample_fmt)/8; |
719 | int isize= av_get_bits_per_sample_fmt(dec->sample_fmt)/8; |
||
720 | 15bfe412 | Michael Niedermayer | const int coded_bps = av_get_bits_per_sample(enc->codec->id); |
721 | |||
722 | 7a086a85 | Michael Niedermayer | need_realloc:
|
723 | audio_buf_size= (allocated_for_size + isize*dec->channels - 1) / (isize*dec->channels);
|
||
724 | 15bfe412 | Michael Niedermayer | audio_buf_size= (audio_buf_size*enc->sample_rate + dec->sample_rate) / dec->sample_rate; |
725 | 8b484d0f | Vitor Sessak | audio_buf_size= audio_buf_size*2 + 10000; //safety factors for the deprecated resampling API |
726 | 79c85beb | Justin Ruggles | audio_buf_size= FFMAX(audio_buf_size, enc->frame_size); |
727 | 15bfe412 | Michael Niedermayer | audio_buf_size*= osize*enc->channels; |
728 | |||
729 | audio_out_size= FFMAX(audio_buf_size, enc->frame_size * osize * enc->channels); |
||
730 | if(coded_bps > 8*osize) |
||
731 | audio_out_size= audio_out_size * coded_bps / (8*osize);
|
||
732 | audio_out_size += FF_MIN_BUFFER_SIZE; |
||
733 | |||
734 | if(audio_out_size > INT_MAX || audio_buf_size > INT_MAX){
|
||
735 | fprintf(stderr, "Buffer sizes too large\n");
|
||
736 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
737 | 15bfe412 | Michael Niedermayer | } |
738 | 85f07f22 | Fabrice Bellard | |
739 | b8919a30 | Michael Niedermayer | av_fast_malloc(&audio_buf, &allocated_audio_buf_size, audio_buf_size); |
740 | av_fast_malloc(&audio_out, &allocated_audio_out_size, audio_out_size); |
||
741 | e185a2f6 | Michael Niedermayer | if (!audio_buf || !audio_out){
|
742 | fprintf(stderr, "Out of memory in do_audio_out\n");
|
||
743 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
744 | e185a2f6 | Michael Niedermayer | } |
745 | d66c7abc | Sylvain Corré | |
746 | 2886f311 | Andreas Öman | if (enc->channels != dec->channels)
|
747 | ost->audio_resample = 1;
|
||
748 | |||
749 | 8afab686 | Stefano Sabatini | resample_changed = ost->resample_sample_fmt != dec->sample_fmt || |
750 | ost->resample_channels != dec->channels || |
||
751 | ost->resample_sample_rate != dec->sample_rate; |
||
752 | |||
753 | if ((ost->audio_resample && !ost->resample) || resample_changed) {
|
||
754 | if (resample_changed) {
|
||
755 | 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", |
||
756 | ist->file_index, ist->index, |
||
757 | ost->resample_sample_rate, av_get_sample_fmt_name(ost->resample_sample_fmt), ost->resample_channels, |
||
758 | dec->sample_rate, av_get_sample_fmt_name(dec->sample_fmt), dec->channels); |
||
759 | ost->resample_sample_fmt = dec->sample_fmt; |
||
760 | ost->resample_channels = dec->channels; |
||
761 | ost->resample_sample_rate = dec->sample_rate; |
||
762 | if (ost->resample)
|
||
763 | audio_resample_close(ost->resample); |
||
764 | } |
||
765 | 07b48f8c | Stefano Sabatini | /* if audio_sync_method is >1 the resampler is needed for audio drift compensation */
|
766 | if (audio_sync_method <= 1 && |
||
767 | ost->resample_sample_fmt == enc->sample_fmt && |
||
768 | 8afab686 | Stefano Sabatini | ost->resample_channels == enc->channels && |
769 | ost->resample_sample_rate == enc->sample_rate) { |
||
770 | ost->resample = NULL;
|
||
771 | ost->audio_resample = 0;
|
||
772 | } else {
|
||
773 | f6715848 | Stefano Sabatini | if (dec->sample_fmt != AV_SAMPLE_FMT_S16)
|
774 | fprintf(stderr, "Warning, using s16 intermediate sample format for resampling\n");
|
||
775 | ost->resample = av_audio_resample_init(enc->channels, dec->channels, |
||
776 | enc->sample_rate, dec->sample_rate, |
||
777 | enc->sample_fmt, dec->sample_fmt, |
||
778 | 16, 10, 0, 0.8); |
||
779 | if (!ost->resample) {
|
||
780 | fprintf(stderr, "Can not resample %d channels @ %d Hz to %d channels @ %d Hz\n",
|
||
781 | dec->channels, dec->sample_rate, |
||
782 | enc->channels, enc->sample_rate); |
||
783 | ffmpeg_exit(1);
|
||
784 | } |
||
785 | 8afab686 | Stefano Sabatini | } |
786 | 2886f311 | Andreas Öman | } |
787 | |||
788 | 5d6e4c16 | Stefano Sabatini | #define MAKE_SFMT_PAIR(a,b) ((a)+AV_SAMPLE_FMT_NB*(b))
|
789 | d1e3c6fd | Baptiste Coudurier | if (!ost->audio_resample && dec->sample_fmt!=enc->sample_fmt &&
|
790 | a79db0f7 | Peter Ross | MAKE_SFMT_PAIR(enc->sample_fmt,dec->sample_fmt)!=ost->reformat_pair) { |
791 | if (ost->reformat_ctx)
|
||
792 | av_audio_convert_free(ost->reformat_ctx); |
||
793 | ost->reformat_ctx = av_audio_convert_alloc(enc->sample_fmt, 1,
|
||
794 | dec->sample_fmt, 1, NULL, 0); |
||
795 | if (!ost->reformat_ctx) {
|
||
796 | fprintf(stderr, "Cannot convert %s sample format to %s sample format\n",
|
||
797 | ba7d6e79 | Stefano Sabatini | av_get_sample_fmt_name(dec->sample_fmt), |
798 | av_get_sample_fmt_name(enc->sample_fmt)); |
||
799 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
800 | a79db0f7 | Peter Ross | } |
801 | ost->reformat_pair=MAKE_SFMT_PAIR(enc->sample_fmt,dec->sample_fmt); |
||
802 | } |
||
803 | |||
804 | 986ebcdb | Michael Niedermayer | if(audio_sync_method){
|
805 | 115329f1 | Diego Biurrun | double delta = get_sync_ipts(ost) * enc->sample_rate - ost->sync_opts
|
806 | 37f5a713 | Justin Ruggles | - av_fifo_size(ost->fifo)/(enc->channels * 2);
|
807 | double idelta= delta*dec->sample_rate / enc->sample_rate;
|
||
808 | ff19d16b | Justin Ruggles | int byte_delta= ((int)idelta)*2*dec->channels; |
809 | ff4905a5 | Michael Niedermayer | |
810 | 986ebcdb | Michael Niedermayer | //FIXME resample delay
|
811 | if(fabs(delta) > 50){ |
||
812 | d4d226a8 | Michael Niedermayer | if(ist->is_start || fabs(delta) > audio_drift_threshold*enc->sample_rate){
|
813 | ff4905a5 | Michael Niedermayer | if(byte_delta < 0){ |
814 | f41dd5aa | Michael Niedermayer | byte_delta= FFMAX(byte_delta, -size); |
815 | ff4905a5 | Michael Niedermayer | size += byte_delta; |
816 | buf -= byte_delta; |
||
817 | if(verbose > 2) |
||
818 | fprintf(stderr, "discarding %d audio samples\n", (int)-delta); |
||
819 | if(!size)
|
||
820 | return;
|
||
821 | ist->is_start=0;
|
||
822 | }else{
|
||
823 | static uint8_t *input_tmp= NULL; |
||
824 | input_tmp= av_realloc(input_tmp, byte_delta + size); |
||
825 | |||
826 | 7a086a85 | Michael Niedermayer | if(byte_delta > allocated_for_size - size){
|
827 | allocated_for_size= byte_delta + (int64_t)size; |
||
828 | goto need_realloc;
|
||
829 | } |
||
830 | ist->is_start=0;
|
||
831 | ff4905a5 | Michael Niedermayer | |
832 | memset(input_tmp, 0, byte_delta);
|
||
833 | memcpy(input_tmp + byte_delta, buf, size); |
||
834 | buf= input_tmp; |
||
835 | size += byte_delta; |
||
836 | if(verbose > 2) |
||
837 | fprintf(stderr, "adding %d audio samples of silence\n", (int)delta); |
||
838 | } |
||
839 | }else if(audio_sync_method>1){ |
||
840 | f66e4f5f | Reimar Döffinger | int comp= av_clip(delta, -audio_sync_method, audio_sync_method);
|
841 | b926b628 | Michael Niedermayer | av_assert0(ost->audio_resample); |
842 | ff4905a5 | Michael Niedermayer | if(verbose > 2) |
843 | fprintf(stderr, "compensating audio timestamp drift:%f compensation:%d in:%d\n", delta, comp, enc->sample_rate);
|
||
844 | 41dd680d | Michael Niedermayer | // 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));
|
845 | ff4905a5 | Michael Niedermayer | av_resample_compensate(*(struct AVResampleContext**)ost->resample, comp, enc->sample_rate);
|
846 | } |
||
847 | 115329f1 | Diego Biurrun | } |
848 | 986ebcdb | Michael Niedermayer | }else
|
849 | b4a3389e | Wolfram Gloger | ost->sync_opts= lrintf(get_sync_ipts(ost) * enc->sample_rate) |
850 | 37f5a713 | Justin Ruggles | - av_fifo_size(ost->fifo)/(enc->channels * 2); //FIXME wrong |
851 | 85f07f22 | Fabrice Bellard | |
852 | if (ost->audio_resample) {
|
||
853 | buftmp = audio_buf; |
||
854 | 115329f1 | Diego Biurrun | size_out = audio_resample(ost->resample, |
855 | 85f07f22 | Fabrice Bellard | (short *)buftmp, (short *)buf, |
856 | 37f5a713 | Justin Ruggles | size / (dec->channels * isize)); |
857 | 287ba997 | Peter Ross | size_out = size_out * enc->channels * osize; |
858 | 85f07f22 | Fabrice Bellard | } else {
|
859 | buftmp = buf; |
||
860 | size_out = size; |
||
861 | } |
||
862 | |||
863 | d1e3c6fd | Baptiste Coudurier | if (!ost->audio_resample && dec->sample_fmt!=enc->sample_fmt) {
|
864 | a79db0f7 | Peter Ross | const void *ibuf[6]= {buftmp}; |
865 | 80f47250 | Michael Niedermayer | void *obuf[6]= {audio_buf}; |
866 | 287ba997 | Peter Ross | int istride[6]= {isize}; |
867 | int ostride[6]= {osize}; |
||
868 | a79db0f7 | Peter Ross | int len= size_out/istride[0]; |
869 | if (av_audio_convert(ost->reformat_ctx, obuf, ostride, ibuf, istride, len)<0) { |
||
870 | printf("av_audio_convert() failed\n");
|
||
871 | f2abc559 | Baptiste Coudurier | if (exit_on_error)
|
872 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
873 | a79db0f7 | Peter Ross | return;
|
874 | } |
||
875 | 80f47250 | Michael Niedermayer | buftmp = audio_buf; |
876 | 287ba997 | Peter Ross | size_out = len*osize; |
877 | a79db0f7 | Peter Ross | } |
878 | |||
879 | 85f07f22 | Fabrice Bellard | /* now encode as many frames as possible */
|
880 | a0663ba4 | Fabrice Bellard | if (enc->frame_size > 1) { |
881 | 85f07f22 | Fabrice Bellard | /* output resampled raw samples */
|
882 | 41dd680d | Michael Niedermayer | if (av_fifo_realloc2(ost->fifo, av_fifo_size(ost->fifo) + size_out) < 0) { |
883 | 745b39d5 | Stefano Sabatini | fprintf(stderr, "av_fifo_realloc2() failed\n");
|
884 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
885 | 745b39d5 | Stefano Sabatini | } |
886 | 41dd680d | Michael Niedermayer | av_fifo_generic_write(ost->fifo, buftmp, size_out, NULL);
|
887 | 85f07f22 | Fabrice Bellard | |
888 | 287ba997 | Peter Ross | frame_bytes = enc->frame_size * osize * enc->channels; |
889 | 115329f1 | Diego Biurrun | |
890 | 41dd680d | Michael Niedermayer | while (av_fifo_size(ost->fifo) >= frame_bytes) {
|
891 | e928649b | Michael Niedermayer | AVPacket pkt; |
892 | av_init_packet(&pkt); |
||
893 | |||
894 | 3898eed8 | Reimar Döffinger | av_fifo_generic_read(ost->fifo, audio_buf, frame_bytes, NULL);
|
895 | 0871ae1a | Michael Niedermayer | |
896 | 5bc440e7 | Michael Niedermayer | //FIXME pass ost->sync_opts as AVFrame.pts in avcodec_encode_audio()
|
897 | |||
898 | 115329f1 | Diego Biurrun | ret = avcodec_encode_audio(enc, audio_out, audio_out_size, |
899 | a0663ba4 | Fabrice Bellard | (short *)audio_buf);
|
900 | 528271ff | Michael Niedermayer | if (ret < 0) { |
901 | fprintf(stderr, "Audio encoding failed\n");
|
||
902 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
903 | 528271ff | Michael Niedermayer | } |
904 | 1008ceb3 | Michael Niedermayer | audio_size += ret; |
905 | e928649b | Michael Niedermayer | pkt.stream_index= ost->index; |
906 | pkt.data= audio_out; |
||
907 | pkt.size= ret; |
||
908 | e7902f20 | Michael Niedermayer | if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
|
909 | c0df9d75 | Michael Niedermayer | pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base); |
910 | cc947f04 | Jean-Daniel Dupas | pkt.flags |= AV_PKT_FLAG_KEY; |
911 | 0b6d358a | Nicolas George | write_frame(s, &pkt, enc, ost->bitstream_filters); |
912 | 115329f1 | Diego Biurrun | |
913 | 986ebcdb | Michael Niedermayer | ost->sync_opts += enc->frame_size; |
914 | 85f07f22 | Fabrice Bellard | } |
915 | } else {
|
||
916 | e928649b | Michael Niedermayer | AVPacket pkt; |
917 | av_init_packet(&pkt); |
||
918 | 986ebcdb | Michael Niedermayer | |
919 | 287ba997 | Peter Ross | ost->sync_opts += size_out / (osize * enc->channels); |
920 | 986ebcdb | Michael Niedermayer | |
921 | a0663ba4 | Fabrice Bellard | /* output a pcm frame */
|
922 | 287ba997 | Peter Ross | /* determine the size of the coded buffer */
|
923 | size_out /= osize; |
||
924 | if (coded_bps)
|
||
925 | 060b8592 | Michael Niedermayer | size_out = size_out*coded_bps/8;
|
926 | 287ba997 | Peter Ross | |
927 | 5ee05a62 | Michael Niedermayer | if(size_out > audio_out_size){
|
928 | fprintf(stderr, "Internal error, buffer size too small\n");
|
||
929 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
930 | 5ee05a62 | Michael Niedermayer | } |
931 | |||
932 | 5bc440e7 | Michael Niedermayer | //FIXME pass ost->sync_opts as AVFrame.pts in avcodec_encode_audio()
|
933 | 115329f1 | Diego Biurrun | ret = avcodec_encode_audio(enc, audio_out, size_out, |
934 | bb270c08 | Diego Biurrun | (short *)buftmp);
|
935 | 528271ff | Michael Niedermayer | if (ret < 0) { |
936 | fprintf(stderr, "Audio encoding failed\n");
|
||
937 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
938 | 528271ff | Michael Niedermayer | } |
939 | 1008ceb3 | Michael Niedermayer | audio_size += ret; |
940 | e928649b | Michael Niedermayer | pkt.stream_index= ost->index; |
941 | pkt.data= audio_out; |
||
942 | pkt.size= ret; |
||
943 | e7902f20 | Michael Niedermayer | if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
|
944 | c0df9d75 | Michael Niedermayer | pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base); |
945 | cc947f04 | Jean-Daniel Dupas | pkt.flags |= AV_PKT_FLAG_KEY; |
946 | 0b6d358a | Nicolas George | write_frame(s, &pkt, enc, ost->bitstream_filters); |
947 | 85f07f22 | Fabrice Bellard | } |
948 | } |
||
949 | |||
950 | 10d104e4 | Philip Gladstone | static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp) |
951 | { |
||
952 | AVCodecContext *dec; |
||
953 | AVPicture *picture2; |
||
954 | AVPicture picture_tmp; |
||
955 | 0c1a9eda | Zdenek Kabelac | uint8_t *buf = 0;
|
956 | 10d104e4 | Philip Gladstone | |
957 | 01f4895c | Michael Niedermayer | dec = ist->st->codec; |
958 | 10d104e4 | Philip Gladstone | |
959 | /* deinterlace : must be done before any resize */
|
||
960 | fdf11906 | Diego Biurrun | if (do_deinterlace) {
|
961 | 10d104e4 | Philip Gladstone | int size;
|
962 | |||
963 | /* create temporary picture */
|
||
964 | size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height); |
||
965 | buf = av_malloc(size); |
||
966 | if (!buf)
|
||
967 | return;
|
||
968 | 115329f1 | Diego Biurrun | |
969 | 10d104e4 | Philip Gladstone | picture2 = &picture_tmp; |
970 | avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height); |
||
971 | |||
972 | 9e1cc598 | Ramiro Polla | if(avpicture_deinterlace(picture2, picture,
|
973 | dec->pix_fmt, dec->width, dec->height) < 0) {
|
||
974 | /* if error, do not deinterlace */
|
||
975 | fprintf(stderr, "Deinterlacing failed\n");
|
||
976 | av_free(buf); |
||
977 | buf = NULL;
|
||
978 | picture2 = picture; |
||
979 | } |
||
980 | 10d104e4 | Philip Gladstone | } else {
|
981 | picture2 = picture; |
||
982 | } |
||
983 | |||
984 | if (picture != picture2)
|
||
985 | *picture = *picture2; |
||
986 | *bufp = buf; |
||
987 | } |
||
988 | |||
989 | ec5517d5 | Fabrice Bellard | /* we begin to correct av delay at this threshold */
|
990 | #define AV_DELAY_MAX 0.100 |
||
991 | 85f07f22 | Fabrice Bellard | |
992 | 115329f1 | Diego Biurrun | static void do_subtitle_out(AVFormatContext *s, |
993 | AVOutputStream *ost, |
||
994 | cf7fc795 | Fabrice Bellard | AVInputStream *ist, |
995 | AVSubtitle *sub, |
||
996 | int64_t pts) |
||
997 | { |
||
998 | static uint8_t *subtitle_out = NULL; |
||
999 | 7f4fca03 | Reimar Döffinger | int subtitle_out_max_size = 1024 * 1024; |
1000 | cf7fc795 | Fabrice Bellard | int subtitle_out_size, nb, i;
|
1001 | AVCodecContext *enc; |
||
1002 | AVPacket pkt; |
||
1003 | |||
1004 | if (pts == AV_NOPTS_VALUE) {
|
||
1005 | fprintf(stderr, "Subtitle packets must have a pts\n");
|
||
1006 | f2abc559 | Baptiste Coudurier | if (exit_on_error)
|
1007 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
1008 | cf7fc795 | Fabrice Bellard | return;
|
1009 | } |
||
1010 | |||
1011 | 01f4895c | Michael Niedermayer | enc = ost->st->codec; |
1012 | cf7fc795 | Fabrice Bellard | |
1013 | if (!subtitle_out) {
|
||
1014 | subtitle_out = av_malloc(subtitle_out_max_size); |
||
1015 | } |
||
1016 | |||
1017 | /* Note: DVB subtitle need one packet to draw them and one other
|
||
1018 | packet to clear them */
|
||
1019 | /* XXX: signal it in the codec context ? */
|
||
1020 | if (enc->codec_id == CODEC_ID_DVB_SUBTITLE)
|
||
1021 | nb = 2;
|
||
1022 | else
|
||
1023 | nb = 1;
|
||
1024 | |||
1025 | for(i = 0; i < nb; i++) { |
||
1026 | 4bbe788a | Reimar Döffinger | sub->pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q); |
1027 | 8b03c014 | Reimar Döffinger | // start_display_time is required to be 0
|
1028 | sub->pts += av_rescale_q(sub->start_display_time, (AVRational){1, 1000}, AV_TIME_BASE_Q); |
||
1029 | sub->end_display_time -= sub->start_display_time; |
||
1030 | sub->start_display_time = 0;
|
||
1031 | 115329f1 | Diego Biurrun | subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out, |
1032 | cf7fc795 | Fabrice Bellard | subtitle_out_max_size, sub); |
1033 | 266649a5 | Reimar Döffinger | if (subtitle_out_size < 0) { |
1034 | fprintf(stderr, "Subtitle encoding failed\n");
|
||
1035 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
1036 | 266649a5 | Reimar Döffinger | } |
1037 | 115329f1 | Diego Biurrun | |
1038 | cf7fc795 | Fabrice Bellard | av_init_packet(&pkt); |
1039 | pkt.stream_index = ost->index; |
||
1040 | pkt.data = subtitle_out; |
||
1041 | pkt.size = subtitle_out_size; |
||
1042 | 8b03c014 | Reimar Döffinger | pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base); |
1043 | cf7fc795 | Fabrice Bellard | if (enc->codec_id == CODEC_ID_DVB_SUBTITLE) {
|
1044 | /* XXX: the pts correction is handled here. Maybe handling
|
||
1045 | it in the codec would be better */
|
||
1046 | if (i == 0) |
||
1047 | pkt.pts += 90 * sub->start_display_time;
|
||
1048 | else
|
||
1049 | pkt.pts += 90 * sub->end_display_time;
|
||
1050 | } |
||
1051 | 0b6d358a | Nicolas George | write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters); |
1052 | cf7fc795 | Fabrice Bellard | } |
1053 | } |
||
1054 | |||
1055 | 8a6cb114 | Michael Niedermayer | static int bit_buffer_size= 1024*256; |
1056 | 27537106 | Michael Niedermayer | static uint8_t *bit_buffer= NULL; |
1057 | 1ff93ffc | Todd Kirby | |
1058 | 115329f1 | Diego Biurrun | static void do_video_out(AVFormatContext *s, |
1059 | AVOutputStream *ost, |
||
1060 | 85f07f22 | Fabrice Bellard | AVInputStream *ist, |
1061 | 7a0f9d7e | Fabrice Bellard | AVFrame *in_picture, |
1062 | 986ebcdb | Michael Niedermayer | int *frame_size)
|
1063 | 85f07f22 | Fabrice Bellard | { |
1064 | 9aa797cd | Stefano Sabatini | int nb_frames, i, ret, resample_changed;
|
1065 | f38d900f | Stefano Sabatini | AVFrame *final_picture, *formatted_picture, *resampling_dst; |
1066 | cfcf0ffd | Fabrice Bellard | AVCodecContext *enc, *dec; |
1067 | 47b229db | Alexander Strange | double sync_ipts;
|
1068 | 115329f1 | Diego Biurrun | |
1069 | 01f4895c | Michael Niedermayer | enc = ost->st->codec; |
1070 | dec = ist->st->codec; |
||
1071 | 85f07f22 | Fabrice Bellard | |
1072 | 47b229db | Alexander Strange | sync_ipts = get_sync_ipts(ost) / av_q2d(enc->time_base); |
1073 | |||
1074 | ec5517d5 | Fabrice Bellard | /* by default, we output a single frame */
|
1075 | nb_frames = 1;
|
||
1076 | |||
1077 | 204c0f48 | Philip Gladstone | *frame_size = 0;
|
1078 | |||
1079 | 9ff261a2 | Michael Niedermayer | if(video_sync_method){
|
1080 | d55065a2 | Alexander Strange | double vdelta = sync_ipts - ost->sync_opts;
|
1081 | e928649b | Michael Niedermayer | //FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
|
1082 | if (vdelta < -1.1) |
||
1083 | nb_frames = 0;
|
||
1084 | e6fdc2b1 | Michael Niedermayer | else if (video_sync_method == 2 || (video_sync_method<0 && (s->oformat->flags & AVFMT_VARIABLE_FPS))){ |
1085 | if(vdelta<=-0.6){ |
||
1086 | nb_frames=0;
|
||
1087 | }else if(vdelta>0.6) |
||
1088 | 7f48bfa1 | Baptiste Coudurier | ost->sync_opts= lrintf(sync_ipts); |
1089 | e6fdc2b1 | Michael Niedermayer | }else if (vdelta > 1.1) |
1090 | 70122f29 | Michael Niedermayer | nb_frames = lrintf(vdelta); |
1091 | fc6765d7 | Michael Niedermayer | //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);
|
1092 | 50c3dd32 | Michael Niedermayer | if (nb_frames == 0){ |
1093 | ++nb_frames_drop; |
||
1094 | if (verbose>2) |
||
1095 | fprintf(stderr, "*** drop!\n");
|
||
1096 | 8300609b | Michael Niedermayer | }else if (nb_frames > 1) { |
1097 | ed30e518 | Michael Niedermayer | nb_frames_dup += nb_frames - 1;
|
1098 | 50c3dd32 | Michael Niedermayer | if (verbose>2) |
1099 | 8300609b | Michael Niedermayer | fprintf(stderr, "*** %d dup!\n", nb_frames-1); |
1100 | 50c3dd32 | Michael Niedermayer | } |
1101 | }else
|
||
1102 | 47b229db | Alexander Strange | ost->sync_opts= lrintf(sync_ipts); |
1103 | 445f1b83 | Roman Shaposhnik | |
1104 | 72415b2a | Stefano Sabatini | nb_frames= FFMIN(nb_frames, max_frames[AVMEDIA_TYPE_VIDEO] - ost->frame_number); |
1105 | 115329f1 | Diego Biurrun | if (nb_frames <= 0) |
1106 | 85f07f22 | Fabrice Bellard | return;
|
1107 | ce7c56c2 | Juanjo | |
1108 | 46847a33 | Michael Niedermayer | formatted_picture = in_picture; |
1109 | 07d0cdfc | Luca Abeni | final_picture = formatted_picture; |
1110 | resampling_dst = &ost->pict_tmp; |
||
1111 | |||
1112 | 9aa797cd | Stefano Sabatini | resample_changed = ost->resample_width != dec->width || |
1113 | ost->resample_height != dec->height || |
||
1114 | ost->resample_pix_fmt != dec->pix_fmt; |
||
1115 | |||
1116 | if (resample_changed) {
|
||
1117 | 2ecc5b70 | Stefano Sabatini | av_log(NULL, AV_LOG_INFO,
|
1118 | "Input stream #%d.%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
|
||
1119 | ist->file_index, ist->index, |
||
1120 | c29c2eea | Stefano Sabatini | ost->resample_width, ost->resample_height, avcodec_get_pix_fmt_name(ost->resample_pix_fmt), |
1121 | dec->width , dec->height , avcodec_get_pix_fmt_name(dec->pix_fmt)); |
||
1122 | b83ccbff | Michael Niedermayer | if(!ost->video_resample)
|
1123 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
1124 | b83ccbff | Michael Niedermayer | } |
1125 | |||
1126 | 46847a33 | Michael Niedermayer | #if !CONFIG_AVFILTER
|
1127 | 85f07f22 | Fabrice Bellard | if (ost->video_resample) {
|
1128 | 34b10a57 | Dieter | final_picture = &ost->pict_tmp; |
1129 | 9aa797cd | Stefano Sabatini | if (resample_changed) {
|
1130 | 352666c1 | Eric Buehl | /* initialize a new scaler context */
|
1131 | sws_freeContext(ost->img_resample_ctx); |
||
1132 | ost->img_resample_ctx = sws_getContext( |
||
1133 | 5879ea6d | Stefano Sabatini | ist->st->codec->width, |
1134 | ist->st->codec->height, |
||
1135 | 352666c1 | Eric Buehl | ist->st->codec->pix_fmt, |
1136 | 0c22311b | Michael Niedermayer | ost->st->codec->width, |
1137 | ost->st->codec->height, |
||
1138 | 352666c1 | Eric Buehl | ost->st->codec->pix_fmt, |
1139 | 1435f2fa | Anton Khirnov | ost->sws_flags, NULL, NULL, NULL); |
1140 | 352666c1 | Eric Buehl | if (ost->img_resample_ctx == NULL) { |
1141 | fprintf(stderr, "Cannot get resampling context\n");
|
||
1142 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
1143 | 352666c1 | Eric Buehl | } |
1144 | } |
||
1145 | 18a54b04 | Luca Abeni | sws_scale(ost->img_resample_ctx, formatted_picture->data, formatted_picture->linesize, |
1146 | 0, ost->resample_height, resampling_dst->data, resampling_dst->linesize);
|
||
1147 | f122deb4 | Luca Abeni | } |
1148 | 46847a33 | Michael Niedermayer | #endif
|
1149 | 07d0cdfc | Luca Abeni | |
1150 | 85f07f22 | Fabrice Bellard | /* duplicates frame if needed */
|
1151 | ec5517d5 | Fabrice Bellard | for(i=0;i<nb_frames;i++) { |
1152 | e928649b | Michael Niedermayer | AVPacket pkt; |
1153 | av_init_packet(&pkt); |
||
1154 | pkt.stream_index= ost->index; |
||
1155 | |||
1156 | e8750b00 | Fred Rothganger | if (s->oformat->flags & AVFMT_RAWPICTURE) {
|
1157 | /* raw pictures are written as AVPicture structure to
|
||
1158 | avoid any copies. We support temorarily the older
|
||
1159 | method. */
|
||
1160 | 2744ca9a | Roman Shaposhnik | AVFrame* old_frame = enc->coded_frame; |
1161 | bb270c08 | Diego Biurrun | enc->coded_frame = dec->coded_frame; //FIXME/XXX remove this hack
|
1162 | e928649b | Michael Niedermayer | pkt.data= (uint8_t *)final_picture; |
1163 | pkt.size= sizeof(AVPicture);
|
||
1164 | b4dba580 | Michael Niedermayer | pkt.pts= av_rescale_q(ost->sync_opts, enc->time_base, ost->st->time_base); |
1165 | cc947f04 | Jean-Daniel Dupas | pkt.flags |= AV_PKT_FLAG_KEY; |
1166 | e928649b | Michael Niedermayer | |
1167 | 0b6d358a | Nicolas George | write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters); |
1168 | bb270c08 | Diego Biurrun | enc->coded_frame = old_frame; |
1169 | e8750b00 | Fred Rothganger | } else {
|
1170 | 492cd3a9 | Michael Niedermayer | AVFrame big_picture; |
1171 | a4d36c11 | Michael Niedermayer | |
1172 | big_picture= *final_picture; |
||
1173 | 7a0f9d7e | Fabrice Bellard | /* better than nothing: use input picture interlaced
|
1174 | settings */
|
||
1175 | big_picture.interlaced_frame = in_picture->interlaced_frame; |
||
1176 | 648e55ff | Anton Khirnov | if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) {
|
1177 | bb198e19 | Michael Niedermayer | if(top_field_first == -1) |
1178 | big_picture.top_field_first = in_picture->top_field_first; |
||
1179 | else
|
||
1180 | 2a8edc5d | Michael Niedermayer | big_picture.top_field_first = top_field_first; |
1181 | bb198e19 | Michael Niedermayer | } |
1182 | 7a0f9d7e | Fabrice Bellard | |
1183 | 85f07f22 | Fabrice Bellard | /* handles sameq here. This is not correct because it may
|
1184 | not be a global option */
|
||
1185 | f62c025a | Stefano Sabatini | big_picture.quality = same_quality ? ist->st->quality : ost->st->quality; |
1186 | f4f3223f | Michael Niedermayer | if(!me_threshold)
|
1187 | big_picture.pict_type = 0;
|
||
1188 | 50c3dd32 | Michael Niedermayer | // big_picture.pts = AV_NOPTS_VALUE;
|
1189 | c0df9d75 | Michael Niedermayer | big_picture.pts= ost->sync_opts; |
1190 | // big_picture.pts= av_rescale(ost->sync_opts, AV_TIME_BASE*(int64_t)enc->time_base.num, enc->time_base.den);
|
||
1191 | 949b1a13 | Steve L'Homme | //av_log(NULL, AV_LOG_DEBUG, "%"PRId64" -> encoder\n", ost->sync_opts);
|
1192 | 4ad08021 | Nicolas George | if (ost->forced_kf_index < ost->forced_kf_count &&
|
1193 | big_picture.pts >= ost->forced_kf_pts[ost->forced_kf_index]) { |
||
1194 | 975a1447 | Stefano Sabatini | big_picture.pict_type = AV_PICTURE_TYPE_I; |
1195 | 4ad08021 | Nicolas George | ost->forced_kf_index++; |
1196 | } |
||
1197 | 115329f1 | Diego Biurrun | ret = avcodec_encode_video(enc, |
1198 | 8a6cb114 | Michael Niedermayer | bit_buffer, bit_buffer_size, |
1199 | 1e491e29 | Michael Niedermayer | &big_picture); |
1200 | 95af5e1c | Michael Niedermayer | if (ret < 0) { |
1201 | 4156a436 | Panagiotis Issaris | fprintf(stderr, "Video encoding failed\n");
|
1202 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
1203 | 4156a436 | Panagiotis Issaris | } |
1204 | 54e28a85 | Baptiste Coudurier | |
1205 | 4bbc6260 | Michael Niedermayer | if(ret>0){ |
1206 | 27537106 | Michael Niedermayer | pkt.data= bit_buffer; |
1207 | e928649b | Michael Niedermayer | pkt.size= ret; |
1208 | e6b4e4ff | Michael Niedermayer | if(enc->coded_frame->pts != AV_NOPTS_VALUE)
|
1209 | c0df9d75 | Michael Niedermayer | pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base); |
1210 | 949b1a13 | Steve L'Homme | /*av_log(NULL, AV_LOG_DEBUG, "encoder -> %"PRId64"/%"PRId64"\n",
|
1211 | c0df9d75 | Michael Niedermayer | pkt.pts != AV_NOPTS_VALUE ? av_rescale(pkt.pts, enc->time_base.den, AV_TIME_BASE*(int64_t)enc->time_base.num) : -1,
|
1212 | pkt.dts != AV_NOPTS_VALUE ? av_rescale(pkt.dts, enc->time_base.den, AV_TIME_BASE*(int64_t)enc->time_base.num) : -1);*/
|
||
1213 | 50c3dd32 | Michael Niedermayer | |
1214 | e6b4e4ff | Michael Niedermayer | if(enc->coded_frame->key_frame)
|
1215 | cc947f04 | Jean-Daniel Dupas | pkt.flags |= AV_PKT_FLAG_KEY; |
1216 | 0b6d358a | Nicolas George | write_frame(s, &pkt, ost->st->codec, ost->bitstream_filters); |
1217 | e928649b | Michael Niedermayer | *frame_size = ret; |
1218 | 44eb047a | Michael Niedermayer | video_size += ret; |
1219 | 54e28a85 | Baptiste Coudurier | //fprintf(stderr,"\nFrame: %3d size: %5d type: %d",
|
1220 | // enc->frame_number-1, ret, enc->pict_type);
|
||
1221 | e928649b | Michael Niedermayer | /* if two pass, output log */
|
1222 | if (ost->logfile && enc->stats_out) {
|
||
1223 | fprintf(ost->logfile, "%s", enc->stats_out);
|
||
1224 | } |
||
1225 | 5abdb4b1 | Fabrice Bellard | } |
1226 | 85f07f22 | Fabrice Bellard | } |
1227 | 50c3dd32 | Michael Niedermayer | ost->sync_opts++; |
1228 | ec5517d5 | Fabrice Bellard | ost->frame_number++; |
1229 | 85f07f22 | Fabrice Bellard | } |
1230 | } |
||
1231 | |||
1232 | 140cb663 | Michael Niedermayer | static double psnr(double d){ |
1233 | b29f97d1 | Zdenek Kabelac | return -10.0*log(d)/log(10.0); |
1234 | 140cb663 | Michael Niedermayer | } |
1235 | |||
1236 | 115329f1 | Diego Biurrun | static void do_video_stats(AVFormatContext *os, AVOutputStream *ost, |
1237 | ec5517d5 | Fabrice Bellard | int frame_size)
|
1238 | ce7c56c2 | Juanjo | { |
1239 | AVCodecContext *enc; |
||
1240 | int frame_number;
|
||
1241 | double ti1, bitrate, avg_bitrate;
|
||
1242 | 115329f1 | Diego Biurrun | |
1243 | b60d1379 | Stefano Sabatini | /* this is executed just the first time do_video_stats is called */
|
1244 | 032aa7df | Stefano Sabatini | if (!vstats_file) {
|
1245 | vstats_file = fopen(vstats_filename, "w");
|
||
1246 | if (!vstats_file) {
|
||
1247 | 4bd0c2b1 | Benoit Fouet | perror("fopen");
|
1248 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
1249 | 4bd0c2b1 | Benoit Fouet | } |
1250 | } |
||
1251 | |||
1252 | 01f4895c | Michael Niedermayer | enc = ost->st->codec; |
1253 | 72415b2a | Stefano Sabatini | if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
|
1254 | ec5517d5 | Fabrice Bellard | frame_number = ost->frame_number; |
1255 | 032aa7df | Stefano Sabatini | fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA); |
1256 | 140cb663 | Michael Niedermayer | if (enc->flags&CODEC_FLAG_PSNR)
|
1257 | 032aa7df | Stefano Sabatini | fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0))); |
1258 | 115329f1 | Diego Biurrun | |
1259 | 032aa7df | Stefano Sabatini | fprintf(vstats_file,"f_size= %6d ", frame_size);
|
1260 | ec5517d5 | Fabrice Bellard | /* compute pts value */
|
1261 | c0df9d75 | Michael Niedermayer | ti1 = ost->sync_opts * av_q2d(enc->time_base); |
1262 | ce7c56c2 | Juanjo | if (ti1 < 0.01) |
1263 | ti1 = 0.01; |
||
1264 | 115329f1 | Diego Biurrun | |
1265 | c0df9d75 | Michael Niedermayer | bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0; |
1266 | 1008ceb3 | Michael Niedermayer | avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0; |
1267 | 032aa7df | Stefano Sabatini | fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
|
1268 | 1008ceb3 | Michael Niedermayer | (double)video_size / 1024, ti1, bitrate, avg_bitrate); |
1269 | 6209669d | Stefano Sabatini | fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type));
|
1270 | ce7c56c2 | Juanjo | } |
1271 | ec5517d5 | Fabrice Bellard | } |
1272 | |||
1273 | b29f97d1 | Zdenek Kabelac | static void print_report(AVFormatContext **output_files, |
1274 | bb270c08 | Diego Biurrun | AVOutputStream **ost_table, int nb_ostreams,
|
1275 | int is_last_report)
|
||
1276 | ec5517d5 | Fabrice Bellard | { |
1277 | char buf[1024]; |
||
1278 | AVOutputStream *ost; |
||
1279 | b5ee9c23 | Michael Niedermayer | AVFormatContext *oc; |
1280 | 0c1a9eda | Zdenek Kabelac | int64_t total_size; |
1281 | ec5517d5 | Fabrice Bellard | AVCodecContext *enc; |
1282 | int frame_number, vid, i;
|
||
1283 | double bitrate, ti1, pts;
|
||
1284 | 0c1a9eda | Zdenek Kabelac | static int64_t last_time = -1; |
1285 | 0888fd22 | Michael Niedermayer | static int qp_histogram[52]; |
1286 | 115329f1 | Diego Biurrun | |
1287 | ec5517d5 | Fabrice Bellard | if (!is_last_report) {
|
1288 | 0c1a9eda | Zdenek Kabelac | int64_t cur_time; |
1289 | ec5517d5 | Fabrice Bellard | /* display the report every 0.5 seconds */
|
1290 | cur_time = av_gettime(); |
||
1291 | if (last_time == -1) { |
||
1292 | last_time = cur_time; |
||
1293 | return;
|
||
1294 | 115329f1 | Diego Biurrun | } |
1295 | ec5517d5 | Fabrice Bellard | if ((cur_time - last_time) < 500000) |
1296 | return;
|
||
1297 | last_time = cur_time; |
||
1298 | } |
||
1299 | |||
1300 | ce7c56c2 | Juanjo | |
1301 | ec5517d5 | Fabrice Bellard | oc = output_files[0];
|
1302 | |||
1303 | 76aa876e | Anton Khirnov | total_size = avio_size(oc->pb); |
1304 | if(total_size<0) // FIXME improve avio_size() so it works with non seekable output too |
||
1305 | a2704c97 | Anton Khirnov | total_size= avio_tell(oc->pb); |
1306 | 115329f1 | Diego Biurrun | |
1307 | ec5517d5 | Fabrice Bellard | buf[0] = '\0'; |
1308 | ti1 = 1e10;
|
||
1309 | vid = 0;
|
||
1310 | for(i=0;i<nb_ostreams;i++) { |
||
1311 | 5da116a3 | Michael Niedermayer | float q = -1; |
1312 | ec5517d5 | Fabrice Bellard | ost = ost_table[i]; |
1313 | 01f4895c | Michael Niedermayer | enc = ost->st->codec; |
1314 | 5da116a3 | Michael Niedermayer | if (!ost->st->stream_copy && enc->coded_frame)
|
1315 | q = enc->coded_frame->quality/(float)FF_QP2LAMBDA;
|
||
1316 | 72415b2a | Stefano Sabatini | if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
|
1317 | 5da116a3 | Michael Niedermayer | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q); |
1318 | 10d104e4 | Philip Gladstone | } |
1319 | 72415b2a | Stefano Sabatini | if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
|
1320 | 7fc98937 | Limin Wang | float t = (av_gettime()-timer_start) / 1000000.0; |
1321 | |||
1322 | ec5517d5 | Fabrice Bellard | frame_number = ost->frame_number; |
1323 | 7fc98937 | Limin Wang | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3d q=%3.1f ", |
1324 | 5da116a3 | Michael Niedermayer | frame_number, (t>1)?(int)(frame_number/t+0.5) : 0, q); |
1325 | 890972be | Michael Niedermayer | if(is_last_report)
|
1326 | 0ecca7a4 | Michael Niedermayer | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L"); |
1327 | e6b4e4ff | Michael Niedermayer | if(qp_hist){
|
1328 | 0888fd22 | Michael Niedermayer | int j;
|
1329 | 5da116a3 | Michael Niedermayer | int qp = lrintf(q);
|
1330 | 37d3e066 | Aurelien Jacobs | if(qp>=0 && qp<FF_ARRAY_ELEMS(qp_histogram)) |
1331 | 0888fd22 | Michael Niedermayer | qp_histogram[qp]++; |
1332 | for(j=0; j<32; j++) |
||
1333 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log(qp_histogram[j]+1)/log(2))); |
||
1334 | } |
||
1335 | 890972be | Michael Niedermayer | if (enc->flags&CODEC_FLAG_PSNR){
|
1336 | int j;
|
||
1337 | double error, error_sum=0; |
||
1338 | double scale, scale_sum=0; |
||
1339 | char type[3]= {'Y','U','V'}; |
||
1340 | 0ecca7a4 | Michael Niedermayer | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR="); |
1341 | 890972be | Michael Niedermayer | for(j=0; j<3; j++){ |
1342 | if(is_last_report){
|
||
1343 | error= enc->error[j]; |
||
1344 | scale= enc->width*enc->height*255.0*255.0*frame_number; |
||
1345 | }else{
|
||
1346 | error= enc->coded_frame->error[j]; |
||
1347 | scale= enc->width*enc->height*255.0*255.0; |
||
1348 | } |
||
1349 | if(j) scale/=4; |
||
1350 | error_sum += error; |
||
1351 | scale_sum += scale; |
||
1352 | 0ecca7a4 | Michael Niedermayer | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error/scale)); |
1353 | 890972be | Michael Niedermayer | } |
1354 | 0ecca7a4 | Michael Niedermayer | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum/scale_sum)); |
1355 | 890972be | Michael Niedermayer | } |
1356 | ec5517d5 | Fabrice Bellard | vid = 1;
|
1357 | } |
||
1358 | /* compute min output value */
|
||
1359 | 43924f01 | Alex Beregszaszi | pts = (double)ost->st->pts.val * av_q2d(ost->st->time_base);
|
1360 | 5d9827bc | Kareila | if ((pts < ti1) && (pts > 0)) |
1361 | ec5517d5 | Fabrice Bellard | ti1 = pts; |
1362 | } |
||
1363 | if (ti1 < 0.01) |
||
1364 | ti1 = 0.01; |
||
1365 | 115329f1 | Diego Biurrun | |
1366 | d1991f51 | Michael Niedermayer | if (verbose > 0 || is_last_report) { |
1367 | f068206e | Bill Eldridge | bitrate = (double)(total_size * 8) / ti1 / 1000.0; |
1368 | 115329f1 | Diego Biurrun | |
1369 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
|
||
1370 | 475f4d8d | David Bolt | "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
|
1371 | ec5517d5 | Fabrice Bellard | (double)total_size / 1024, ti1, bitrate); |
1372 | a6a92a9a | Wolfram Gloger | |
1373 | 0f649d66 | Michael Niedermayer | if (nb_frames_dup || nb_frames_drop)
|
1374 | bb270c08 | Diego Biurrun | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d", |
1375 | nb_frames_dup, nb_frames_drop); |
||
1376 | 115329f1 | Diego Biurrun | |
1377 | d8019eb5 | Allen Day | if (verbose >= 0) |
1378 | fprintf(stderr, "%s \r", buf);
|
||
1379 | |||
1380 | ec5517d5 | Fabrice Bellard | fflush(stderr); |
1381 | } |
||
1382 | 115329f1 | Diego Biurrun | |
1383 | 1008ceb3 | Michael Niedermayer | if (is_last_report && verbose >= 0){ |
1384 | int64_t raw= audio_size + video_size + extra_size; |
||
1385 | f068206e | Bill Eldridge | fprintf(stderr, "\n");
|
1386 | 1008ceb3 | Michael Niedermayer | fprintf(stderr, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
|
1387 | video_size/1024.0, |
||
1388 | audio_size/1024.0, |
||
1389 | extra_size/1024.0, |
||
1390 | 100.0*(total_size - raw)/raw |
||
1391 | ); |
||
1392 | } |
||
1393 | ce7c56c2 | Juanjo | } |
1394 | |||
1395 | 14cf0fd2 | Alex Converse | static void generate_silence(uint8_t* buf, enum AVSampleFormat sample_fmt, size_t size) |
1396 | { |
||
1397 | int fill_char = 0x00; |
||
1398 | if (sample_fmt == AV_SAMPLE_FMT_U8)
|
||
1399 | fill_char = 0x80;
|
||
1400 | memset(buf, fill_char, size); |
||
1401 | } |
||
1402 | |||
1403 | a700a6ae | Fabrice Bellard | /* pkt = NULL means EOF (needed to flush decoder buffers) */
|
1404 | static int output_packet(AVInputStream *ist, int ist_index, |
||
1405 | AVOutputStream **ost_table, int nb_ostreams,
|
||
1406 | 4b85a28f | Wolfram Gloger | const AVPacket *pkt)
|
1407 | a700a6ae | Fabrice Bellard | { |
1408 | AVFormatContext *os; |
||
1409 | AVOutputStream *ost; |
||
1410 | ede0e475 | Thilo Borgmann | int ret, i;
|
1411 | cb48fdf6 | Alexander Strange | int got_output;
|
1412 | a700a6ae | Fabrice Bellard | AVFrame picture; |
1413 | 3fd62c6e | Stefano Sabatini | void *buffer_to_free = NULL; |
1414 | f038fe8b | Diego Biurrun | static unsigned int samples_size= 0; |
1415 | cf7fc795 | Fabrice Bellard | AVSubtitle subtitle, *subtitle_to_free; |
1416 | 0ff4f0c0 | Alexander Strange | int64_t pkt_pts = AV_NOPTS_VALUE; |
1417 | 46847a33 | Michael Niedermayer | #if CONFIG_AVFILTER
|
1418 | d21f58b5 | Baptiste Coudurier | int frame_available;
|
1419 | 46847a33 | Michael Niedermayer | #endif
|
1420 | |||
1421 | ede0e475 | Thilo Borgmann | AVPacket avpkt; |
1422 | ba7d6e79 | Stefano Sabatini | int bps = av_get_bits_per_sample_fmt(ist->st->codec->sample_fmt)>>3; |
1423 | ede0e475 | Thilo Borgmann | |
1424 | ed923859 | Michael Niedermayer | if(ist->next_pts == AV_NOPTS_VALUE)
|
1425 | ist->next_pts= ist->pts; |
||
1426 | |||
1427 | a700a6ae | Fabrice Bellard | if (pkt == NULL) { |
1428 | /* EOF handling */
|
||
1429 | 031e14ea | Thilo Borgmann | av_init_packet(&avpkt); |
1430 | ede0e475 | Thilo Borgmann | avpkt.data = NULL;
|
1431 | avpkt.size = 0;
|
||
1432 | a700a6ae | Fabrice Bellard | goto handle_eof;
|
1433 | 031e14ea | Thilo Borgmann | } else {
|
1434 | avpkt = *pkt; |
||
1435 | a700a6ae | Fabrice Bellard | } |
1436 | |||
1437 | b1b818fc | Michael Niedermayer | if(pkt->dts != AV_NOPTS_VALUE)
|
1438 | ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q); |
||
1439 | 0ff4f0c0 | Alexander Strange | if(pkt->pts != AV_NOPTS_VALUE)
|
1440 | pkt_pts = av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q); |
||
1441 | b1b818fc | Michael Niedermayer | |
1442 | bd6754aa | Michael Niedermayer | //while we have more to decode or while the decoder did output something on EOF
|
1443 | cb48fdf6 | Alexander Strange | while (avpkt.size > 0 || (!pkt && got_output)) { |
1444 | 036c1382 | Michael Niedermayer | uint8_t *data_buf, *decoded_data_buf; |
1445 | int data_size, decoded_data_size;
|
||
1446 | a700a6ae | Fabrice Bellard | handle_eof:
|
1447 | b1b818fc | Michael Niedermayer | ist->pts= ist->next_pts; |
1448 | 19d5da50 | Michael Niedermayer | |
1449 | d859bb1d | Sascha Sommer | if(avpkt.size && avpkt.size != pkt->size &&
|
1450 | 6e2fdc3e | Stefano Sabatini | ((!ist->showed_multi_packet_warning && verbose>0) || verbose>1)){ |
1451 | 40cb57a2 | Michael Niedermayer | fprintf(stderr, "Multiple frames in a packet from stream %d\n", pkt->stream_index);
|
1452 | 3ff0daf0 | Michael Niedermayer | ist->showed_multi_packet_warning=1;
|
1453 | } |
||
1454 | 40cb57a2 | Michael Niedermayer | |
1455 | a700a6ae | Fabrice Bellard | /* decode the packet if needed */
|
1456 | 036c1382 | Michael Niedermayer | decoded_data_buf = NULL; /* fail safe */ |
1457 | decoded_data_size= 0;
|
||
1458 | data_buf = avpkt.data; |
||
1459 | data_size = avpkt.size; |
||
1460 | cf7fc795 | Fabrice Bellard | subtitle_to_free = NULL;
|
1461 | a700a6ae | Fabrice Bellard | if (ist->decoding_needed) {
|
1462 | 01f4895c | Michael Niedermayer | switch(ist->st->codec->codec_type) {
|
1463 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_AUDIO:{
|
1464 | 81b060fa | Loren Merritt | if(pkt && samples_size < FFMAX(pkt->size*sizeof(*samples), AVCODEC_MAX_AUDIO_FRAME_SIZE)) { |
1465 | samples_size = FFMAX(pkt->size*sizeof(*samples), AVCODEC_MAX_AUDIO_FRAME_SIZE);
|
||
1466 | av_free(samples); |
||
1467 | samples= av_malloc(samples_size); |
||
1468 | } |
||
1469 | 036c1382 | Michael Niedermayer | decoded_data_size= samples_size; |
1470 | a700a6ae | Fabrice Bellard | /* XXX: could avoid copy if PCM 16 bits with same
|
1471 | endianness as CPU */
|
||
1472 | 036c1382 | Michael Niedermayer | ret = avcodec_decode_audio3(ist->st->codec, samples, &decoded_data_size, |
1473 | ede0e475 | Thilo Borgmann | &avpkt); |
1474 | a700a6ae | Fabrice Bellard | if (ret < 0) |
1475 | goto fail_decode;
|
||
1476 | ede0e475 | Thilo Borgmann | avpkt.data += ret; |
1477 | avpkt.size -= ret; |
||
1478 | 036c1382 | Michael Niedermayer | data_size = ret; |
1479 | cb48fdf6 | Alexander Strange | got_output = decoded_data_size > 0;
|
1480 | a700a6ae | Fabrice Bellard | /* Some bug in mpeg audio decoder gives */
|
1481 | 036c1382 | Michael Niedermayer | /* decoded_data_size < 0, it seems they are overflows */
|
1482 | cb48fdf6 | Alexander Strange | if (!got_output) {
|
1483 | a700a6ae | Fabrice Bellard | /* no audio frame */
|
1484 | continue;
|
||
1485 | } |
||
1486 | 036c1382 | Michael Niedermayer | decoded_data_buf = (uint8_t *)samples; |
1487 | ist->next_pts += ((int64_t)AV_TIME_BASE/bps * decoded_data_size) / |
||
1488 | 01f4895c | Michael Niedermayer | (ist->st->codec->sample_rate * ist->st->codec->channels); |
1489 | df84ac2e | Michael Niedermayer | break;}
|
1490 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_VIDEO:
|
1491 | 036c1382 | Michael Niedermayer | decoded_data_size = (ist->st->codec->width * ist->st->codec->height * 3) / 2; |
1492 | a700a6ae | Fabrice Bellard | /* XXX: allocate picture correctly */
|
1493 | 9740beff | Michael Niedermayer | avcodec_get_frame_defaults(&picture); |
1494 | 6b474953 | Alexander Strange | avpkt.pts = pkt_pts; |
1495 | avpkt.dts = ist->pts; |
||
1496 | 0ff4f0c0 | Alexander Strange | pkt_pts = AV_NOPTS_VALUE; |
1497 | 9740beff | Michael Niedermayer | |
1498 | ede0e475 | Thilo Borgmann | ret = avcodec_decode_video2(ist->st->codec, |
1499 | cb48fdf6 | Alexander Strange | &picture, &got_output, &avpkt); |
1500 | a700a6ae | Fabrice Bellard | ist->st->quality= picture.quality; |
1501 | 115329f1 | Diego Biurrun | if (ret < 0) |
1502 | a700a6ae | Fabrice Bellard | goto fail_decode;
|
1503 | cb48fdf6 | Alexander Strange | if (!got_output) {
|
1504 | a700a6ae | Fabrice Bellard | /* no picture yet */
|
1505 | goto discard_packet;
|
||
1506 | } |
||
1507 | 6b474953 | Alexander Strange | ist->next_pts = ist->pts = guess_correct_pts(&ist->pts_ctx, picture.pkt_pts, picture.pkt_dts); |
1508 | 01f4895c | Michael Niedermayer | if (ist->st->codec->time_base.num != 0) { |
1509 | 3797c74b | Michael Niedermayer | int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame; |
1510 | 115329f1 | Diego Biurrun | ist->next_pts += ((int64_t)AV_TIME_BASE * |
1511 | 34583e1b | Michael Niedermayer | ist->st->codec->time_base.num * ticks) / |
1512 | 01f4895c | Michael Niedermayer | ist->st->codec->time_base.den; |
1513 | a700a6ae | Fabrice Bellard | } |
1514 | ede0e475 | Thilo Borgmann | avpkt.size = 0;
|
1515 | 3fd62c6e | Stefano Sabatini | buffer_to_free = NULL;
|
1516 | pre_process_video_frame(ist, (AVPicture *)&picture, &buffer_to_free); |
||
1517 | a700a6ae | Fabrice Bellard | break;
|
1518 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_SUBTITLE:
|
1519 | ede0e475 | Thilo Borgmann | ret = avcodec_decode_subtitle2(ist->st->codec, |
1520 | cb48fdf6 | Alexander Strange | &subtitle, &got_output, &avpkt); |
1521 | cf7fc795 | Fabrice Bellard | if (ret < 0) |
1522 | a700a6ae | Fabrice Bellard | goto fail_decode;
|
1523 | cb48fdf6 | Alexander Strange | if (!got_output) {
|
1524 | cf7fc795 | Fabrice Bellard | goto discard_packet;
|
1525 | a700a6ae | Fabrice Bellard | } |
1526 | cf7fc795 | Fabrice Bellard | subtitle_to_free = &subtitle; |
1527 | ede0e475 | Thilo Borgmann | avpkt.size = 0;
|
1528 | cf7fc795 | Fabrice Bellard | break;
|
1529 | default:
|
||
1530 | goto fail_decode;
|
||
1531 | } |
||
1532 | } else {
|
||
1533 | bde0705c | Limin Wang | switch(ist->st->codec->codec_type) {
|
1534 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_AUDIO:
|
1535 | bde0705c | Limin Wang | ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) / |
1536 | 1c715415 | Michael Niedermayer | ist->st->codec->sample_rate; |
1537 | bde0705c | Limin Wang | break;
|
1538 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_VIDEO:
|
1539 | bde0705c | Limin Wang | if (ist->st->codec->time_base.num != 0) { |
1540 | 3797c74b | Michael Niedermayer | int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame; |
1541 | bde0705c | Limin Wang | ist->next_pts += ((int64_t)AV_TIME_BASE * |
1542 | 34583e1b | Michael Niedermayer | ist->st->codec->time_base.num * ticks) / |
1543 | bde0705c | Limin Wang | ist->st->codec->time_base.den; |
1544 | 2fef0bdf | Michael Niedermayer | } |
1545 | bde0705c | Limin Wang | break;
|
1546 | a700a6ae | Fabrice Bellard | } |
1547 | ede0e475 | Thilo Borgmann | ret = avpkt.size; |
1548 | avpkt.size = 0;
|
||
1549 | bde0705c | Limin Wang | } |
1550 | a700a6ae | Fabrice Bellard | |
1551 | 46847a33 | Michael Niedermayer | #if CONFIG_AVFILTER
|
1552 | if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ist->input_video_filter) {
|
||
1553 | d9c3e5f6 | Michael Niedermayer | AVRational sar; |
1554 | if (ist->st->sample_aspect_ratio.num) sar = ist->st->sample_aspect_ratio;
|
||
1555 | else sar = ist->st->codec->sample_aspect_ratio;
|
||
1556 | 46847a33 | Michael Niedermayer | // add it to be filtered
|
1557 | av_vsrc_buffer_add_frame(ist->input_video_filter, &picture, |
||
1558 | ist->pts, |
||
1559 | d9c3e5f6 | Michael Niedermayer | sar); |
1560 | 46847a33 | Michael Niedermayer | } |
1561 | #endif
|
||
1562 | |||
1563 | bde0705c | Limin Wang | // preprocess audio (volume)
|
1564 | 72415b2a | Stefano Sabatini | if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
|
1565 | bde0705c | Limin Wang | if (audio_volume != 256) { |
1566 | short *volp;
|
||
1567 | volp = samples; |
||
1568 | 036c1382 | Michael Niedermayer | for(i=0;i<(decoded_data_size / sizeof(short));i++) { |
1569 | bde0705c | Limin Wang | int v = ((*volp) * audio_volume + 128) >> 8; |
1570 | if (v < -32768) v = -32768; |
||
1571 | if (v > 32767) v = 32767; |
||
1572 | *volp++ = v; |
||
1573 | 7e987c33 | Calcium | } |
1574 | } |
||
1575 | bde0705c | Limin Wang | } |
1576 | 7e987c33 | Calcium | |
1577 | bde0705c | Limin Wang | /* frame rate emulation */
|
1578 | 3a25ca18 | Stefano Sabatini | if (rate_emu) {
|
1579 | cb103a19 | Stefano Sabatini | int64_t pts = av_rescale(ist->pts, 1000000, AV_TIME_BASE);
|
1580 | bde0705c | Limin Wang | int64_t now = av_gettime() - ist->start; |
1581 | if (pts > now)
|
||
1582 | usleep(pts - now); |
||
1583 | } |
||
1584 | 46847a33 | Michael Niedermayer | #if CONFIG_AVFILTER
|
1585 | d21f58b5 | Baptiste Coudurier | frame_available = ist->st->codec->codec_type != AVMEDIA_TYPE_VIDEO || |
1586 | dfd57757 | Stefano Sabatini | !ist->output_video_filter || avfilter_poll_frame(ist->output_video_filter->inputs[0]);
|
1587 | 46847a33 | Michael Niedermayer | #endif
|
1588 | bde0705c | Limin Wang | /* if output time reached then transcode raw format,
|
1589 | encode packets and output them */
|
||
1590 | if (start_time == 0 || ist->pts >= start_time) |
||
1591 | 46847a33 | Michael Niedermayer | #if CONFIG_AVFILTER
|
1592 | d21f58b5 | Baptiste Coudurier | while (frame_available) {
|
1593 | ff0652e5 | Stefano Sabatini | AVRational ist_pts_tb; |
1594 | dfd57757 | Stefano Sabatini | if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ist->output_video_filter)
|
1595 | ff0652e5 | Stefano Sabatini | get_filtered_video_frame(ist->output_video_filter, &picture, &ist->picref, &ist_pts_tb); |
1596 | if (ist->picref)
|
||
1597 | da1b9b88 | Stefano Sabatini | ist->pts = av_rescale_q(ist->picref->pts, ist_pts_tb, AV_TIME_BASE_Q); |
1598 | 46847a33 | Michael Niedermayer | #endif
|
1599 | bde0705c | Limin Wang | for(i=0;i<nb_ostreams;i++) { |
1600 | int frame_size;
|
||
1601 | a700a6ae | Fabrice Bellard | |
1602 | bde0705c | Limin Wang | ost = ost_table[i]; |
1603 | if (ost->source_index == ist_index) {
|
||
1604 | os = output_files[ost->file_index]; |
||
1605 | a700a6ae | Fabrice Bellard | |
1606 | bde0705c | Limin Wang | /* set the input output pts pairs */
|
1607 | //ost->sync_ipts = (double)(ist->pts + input_files_ts_offset[ist->file_index] - start_time)/ AV_TIME_BASE;
|
||
1608 | |||
1609 | if (ost->encoding_needed) {
|
||
1610 | b926b628 | Michael Niedermayer | av_assert0(ist->decoding_needed); |
1611 | bde0705c | Limin Wang | switch(ost->st->codec->codec_type) {
|
1612 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_AUDIO:
|
1613 | 036c1382 | Michael Niedermayer | do_audio_out(os, ost, ist, decoded_data_buf, decoded_data_size); |
1614 | bde0705c | Limin Wang | break;
|
1615 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_VIDEO:
|
1616 | 46847a33 | Michael Niedermayer | #if CONFIG_AVFILTER
|
1617 | cc80caff | S.N. Hemanth Meenakshisundaram | if (ist->picref->video)
|
1618 | ost->st->codec->sample_aspect_ratio = ist->picref->video->pixel_aspect; |
||
1619 | 46847a33 | Michael Niedermayer | #endif
|
1620 | bde0705c | Limin Wang | do_video_out(os, ost, ist, &picture, &frame_size); |
1621 | b60d1379 | Stefano Sabatini | if (vstats_filename && frame_size)
|
1622 | bde0705c | Limin Wang | do_video_stats(os, ost, frame_size); |
1623 | break;
|
||
1624 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_SUBTITLE:
|
1625 | bde0705c | Limin Wang | do_subtitle_out(os, ost, ist, &subtitle, |
1626 | pkt->pts); |
||
1627 | break;
|
||
1628 | default:
|
||
1629 | 0f4e8165 | Ronald S. Bultje | abort(); |
1630 | bde0705c | Limin Wang | } |
1631 | } else {
|
||
1632 | AVFrame avframe; //FIXME/XXX remove this
|
||
1633 | AVPacket opkt; |
||
1634 | cdf38a17 | Michael Niedermayer | int64_t ost_tb_start_time= av_rescale_q(start_time, AV_TIME_BASE_Q, ost->st->time_base); |
1635 | |||
1636 | bde0705c | Limin Wang | av_init_packet(&opkt); |
1637 | |||
1638 | cc947f04 | Jean-Daniel Dupas | if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) && !copy_initial_nonkeyframes)
|
1639 | 7cacf1e8 | Michael Niedermayer | continue;
|
1640 | |||
1641 | bde0705c | Limin Wang | /* no reencoding needed : output the packet directly */
|
1642 | /* force the input stream PTS */
|
||
1643 | |||
1644 | avcodec_get_frame_defaults(&avframe); |
||
1645 | ost->st->codec->coded_frame= &avframe; |
||
1646 | cc947f04 | Jean-Daniel Dupas | avframe.key_frame = pkt->flags & AV_PKT_FLAG_KEY; |
1647 | bde0705c | Limin Wang | |
1648 | 72415b2a | Stefano Sabatini | if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
|
1649 | bde0705c | Limin Wang | audio_size += data_size; |
1650 | 72415b2a | Stefano Sabatini | else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { |
1651 | bde0705c | Limin Wang | video_size += data_size; |
1652 | ost->sync_opts++; |
||
1653 | } |
||
1654 | |||
1655 | opkt.stream_index= ost->index; |
||
1656 | if(pkt->pts != AV_NOPTS_VALUE)
|
||
1657 | cdf38a17 | Michael Niedermayer | opkt.pts= av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time; |
1658 | bde0705c | Limin Wang | else
|
1659 | opkt.pts= AV_NOPTS_VALUE; |
||
1660 | |||
1661 | d2ce2f5e | Baptiste Coudurier | if (pkt->dts == AV_NOPTS_VALUE)
|
1662 | 181782ae | Michael Niedermayer | opkt.dts = av_rescale_q(ist->pts, AV_TIME_BASE_Q, ost->st->time_base); |
1663 | d2ce2f5e | Baptiste Coudurier | else
|
1664 | opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base); |
||
1665 | cdf38a17 | Michael Niedermayer | opkt.dts -= ost_tb_start_time; |
1666 | c0dd7b7c | Michael Niedermayer | |
1667 | a03d59b7 | Aurelien Jacobs | opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base); |
1668 | bde0705c | Limin Wang | opkt.flags= pkt->flags; |
1669 | |||
1670 | //FIXME remove the following 2 lines they shall be replaced by the bitstream filters
|
||
1671 | 5bfe91e6 | Michael Niedermayer | if( ost->st->codec->codec_id != CODEC_ID_H264
|
1672 | && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO |
||
1673 | && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO |
||
1674 | ) { |
||
1675 | cc947f04 | Jean-Daniel Dupas | if(av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, data_buf, data_size, pkt->flags & AV_PKT_FLAG_KEY))
|
1676 | 9f907d85 | Michael Niedermayer | opkt.destruct= av_destruct_packet; |
1677 | d310d56a | Baptiste Coudurier | } else {
|
1678 | opkt.data = data_buf; |
||
1679 | opkt.size = data_size; |
||
1680 | } |
||
1681 | bde0705c | Limin Wang | |
1682 | 0b6d358a | Nicolas George | write_frame(os, &opkt, ost->st->codec, ost->bitstream_filters); |
1683 | bde0705c | Limin Wang | ost->st->codec->frame_number++; |
1684 | ost->frame_number++; |
||
1685 | av_free_packet(&opkt); |
||
1686 | a700a6ae | Fabrice Bellard | } |
1687 | } |
||
1688 | bde0705c | Limin Wang | } |
1689 | 46847a33 | Michael Niedermayer | |
1690 | #if CONFIG_AVFILTER
|
||
1691 | 748db0fc | Michael Niedermayer | frame_available = (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) && |
1692 | dfd57757 | Stefano Sabatini | ist->output_video_filter && avfilter_poll_frame(ist->output_video_filter->inputs[0]);
|
1693 | 46847a33 | Michael Niedermayer | if(ist->picref)
|
1694 | 7fce481a | S.N. Hemanth Meenakshisundaram | avfilter_unref_buffer(ist->picref); |
1695 | 46847a33 | Michael Niedermayer | } |
1696 | #endif
|
||
1697 | bde0705c | Limin Wang | av_free(buffer_to_free); |
1698 | /* XXX: allocate the subtitles in the codec ? */
|
||
1699 | if (subtitle_to_free) {
|
||
1700 | 1d6233d3 | Aurelien Jacobs | avsubtitle_free(subtitle_to_free); |
1701 | bde0705c | Limin Wang | subtitle_to_free = NULL;
|
1702 | a700a6ae | Fabrice Bellard | } |
1703 | bde0705c | Limin Wang | } |
1704 | a700a6ae | Fabrice Bellard | discard_packet:
|
1705 | 6f824977 | Michael Niedermayer | if (pkt == NULL) { |
1706 | /* EOF handling */
|
||
1707 | 115329f1 | Diego Biurrun | |
1708 | 6f824977 | Michael Niedermayer | for(i=0;i<nb_ostreams;i++) { |
1709 | ost = ost_table[i]; |
||
1710 | if (ost->source_index == ist_index) {
|
||
1711 | 01f4895c | Michael Niedermayer | AVCodecContext *enc= ost->st->codec; |
1712 | 6f824977 | Michael Niedermayer | os = output_files[ost->file_index]; |
1713 | 115329f1 | Diego Biurrun | |
1714 | 72415b2a | Stefano Sabatini | if(ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <=1) |
1715 | 6f824977 | Michael Niedermayer | continue;
|
1716 | 72415b2a | Stefano Sabatini | if(ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE))
|
1717 | 6f824977 | Michael Niedermayer | continue;
|
1718 | |||
1719 | if (ost->encoding_needed) {
|
||
1720 | for(;;) {
|
||
1721 | AVPacket pkt; |
||
1722 | cef7cc72 | Justin Ruggles | int fifo_bytes;
|
1723 | 6f824977 | Michael Niedermayer | av_init_packet(&pkt); |
1724 | pkt.stream_index= ost->index; |
||
1725 | 115329f1 | Diego Biurrun | |
1726 | 01f4895c | Michael Niedermayer | switch(ost->st->codec->codec_type) {
|
1727 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_AUDIO:
|
1728 | 41dd680d | Michael Niedermayer | fifo_bytes = av_fifo_size(ost->fifo); |
1729 | cef7cc72 | Justin Ruggles | ret = 0;
|
1730 | /* encode any samples remaining in fifo */
|
||
1731 | b10d7e4e | Baptiste Coudurier | if (fifo_bytes > 0) { |
1732 | ba7d6e79 | Stefano Sabatini | int osize = av_get_bits_per_sample_fmt(enc->sample_fmt) >> 3; |
1733 | cef7cc72 | Justin Ruggles | int fs_tmp = enc->frame_size;
|
1734 | b10d7e4e | Baptiste Coudurier | |
1735 | 79c85beb | Justin Ruggles | av_fifo_generic_read(ost->fifo, audio_buf, fifo_bytes, NULL);
|
1736 | b10d7e4e | Baptiste Coudurier | if (enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
|
1737 | enc->frame_size = fifo_bytes / (osize * enc->channels); |
||
1738 | } else { /* pad */ |
||
1739 | int frame_bytes = enc->frame_size*osize*enc->channels;
|
||
1740 | 79c85beb | Justin Ruggles | if (allocated_audio_buf_size < frame_bytes)
|
1741 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
1742 | 14cf0fd2 | Alex Converse | generate_silence(audio_buf+fifo_bytes, enc->sample_fmt, frame_bytes - fifo_bytes); |
1743 | b10d7e4e | Baptiste Coudurier | } |
1744 | |||
1745 | 79c85beb | Justin Ruggles | ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, (short *)audio_buf);
|
1746 | 7c8689ef | Baptiste Coudurier | pkt.duration = av_rescale((int64_t)enc->frame_size*ost->st->time_base.den, |
1747 | ost->st->time_base.num, enc->sample_rate); |
||
1748 | cef7cc72 | Justin Ruggles | enc->frame_size = fs_tmp; |
1749 | 9e0db7d5 | Michael Niedermayer | } |
1750 | if(ret <= 0) { |
||
1751 | cef7cc72 | Justin Ruggles | ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);
|
1752 | } |
||
1753 | 528271ff | Michael Niedermayer | if (ret < 0) { |
1754 | fprintf(stderr, "Audio encoding failed\n");
|
||
1755 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
1756 | 528271ff | Michael Niedermayer | } |
1757 | 6f824977 | Michael Niedermayer | audio_size += ret; |
1758 | cc947f04 | Jean-Daniel Dupas | pkt.flags |= AV_PKT_FLAG_KEY; |
1759 | 6f824977 | Michael Niedermayer | break;
|
1760 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_VIDEO:
|
1761 | 8a6cb114 | Michael Niedermayer | ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);
|
1762 | 528271ff | Michael Niedermayer | if (ret < 0) { |
1763 | fprintf(stderr, "Video encoding failed\n");
|
||
1764 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
1765 | 528271ff | Michael Niedermayer | } |
1766 | 6f824977 | Michael Niedermayer | video_size += ret; |
1767 | if(enc->coded_frame && enc->coded_frame->key_frame)
|
||
1768 | cc947f04 | Jean-Daniel Dupas | pkt.flags |= AV_PKT_FLAG_KEY; |
1769 | 6f824977 | Michael Niedermayer | if (ost->logfile && enc->stats_out) {
|
1770 | fprintf(ost->logfile, "%s", enc->stats_out);
|
||
1771 | } |
||
1772 | break;
|
||
1773 | default:
|
||
1774 | ret=-1;
|
||
1775 | } |
||
1776 | 115329f1 | Diego Biurrun | |
1777 | 6f824977 | Michael Niedermayer | if(ret<=0) |
1778 | break;
|
||
1779 | 27537106 | Michael Niedermayer | pkt.data= bit_buffer; |
1780 | 6f824977 | Michael Niedermayer | pkt.size= ret; |
1781 | e7902f20 | Michael Niedermayer | if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
|
1782 | c0df9d75 | Michael Niedermayer | pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base); |
1783 | 0b6d358a | Nicolas George | write_frame(os, &pkt, ost->st->codec, ost->bitstream_filters); |
1784 | 6f824977 | Michael Niedermayer | } |
1785 | } |
||
1786 | } |
||
1787 | } |
||
1788 | } |
||
1789 | 115329f1 | Diego Biurrun | |
1790 | a700a6ae | Fabrice Bellard | return 0; |
1791 | fail_decode:
|
||
1792 | return -1; |
||
1793 | } |
||
1794 | |||
1795 | 6d1ba1ac | Luca Abeni | static void print_sdp(AVFormatContext **avc, int n) |
1796 | { |
||
1797 | char sdp[2048]; |
||
1798 | |||
1799 | c3675dfe | Anton Khirnov | av_sdp_create(avc, n, sdp, sizeof(sdp));
|
1800 | 6d1ba1ac | Luca Abeni | printf("SDP:\n%s\n", sdp);
|
1801 | 536cd1db | Luca Barbato | fflush(stdout); |
1802 | 6d1ba1ac | Luca Abeni | } |
1803 | a700a6ae | Fabrice Bellard | |
1804 | 7a39f142 | Anton Khirnov | static int copy_chapters(int infile, int outfile) |
1805 | { |
||
1806 | AVFormatContext *is = input_files[infile]; |
||
1807 | AVFormatContext *os = output_files[outfile]; |
||
1808 | int i;
|
||
1809 | |||
1810 | for (i = 0; i < is->nb_chapters; i++) { |
||
1811 | AVChapter *in_ch = is->chapters[i], *out_ch; |
||
1812 | int64_t ts_off = av_rescale_q(start_time - input_files_ts_offset[infile], |
||
1813 | AV_TIME_BASE_Q, in_ch->time_base); |
||
1814 | int64_t rt = (recording_time == INT64_MAX) ? INT64_MAX : |
||
1815 | av_rescale_q(recording_time, AV_TIME_BASE_Q, in_ch->time_base); |
||
1816 | |||
1817 | |||
1818 | if (in_ch->end < ts_off)
|
||
1819 | continue;
|
||
1820 | if (rt != INT64_MAX && in_ch->start > rt + ts_off)
|
||
1821 | break;
|
||
1822 | |||
1823 | out_ch = av_mallocz(sizeof(AVChapter));
|
||
1824 | if (!out_ch)
|
||
1825 | return AVERROR(ENOMEM);
|
||
1826 | |||
1827 | out_ch->id = in_ch->id; |
||
1828 | out_ch->time_base = in_ch->time_base; |
||
1829 | out_ch->start = FFMAX(0, in_ch->start - ts_off);
|
||
1830 | out_ch->end = FFMIN(rt, in_ch->end - ts_off); |
||
1831 | |||
1832 | d0abe80a | Anton Khirnov | if (metadata_chapters_autocopy)
|
1833 | 7f88a5bf | Ronald S. Bultje | av_metadata_copy(&out_ch->metadata, in_ch->metadata, 0);
|
1834 | 7a39f142 | Anton Khirnov | |
1835 | os->nb_chapters++; |
||
1836 | os->chapters = av_realloc(os->chapters, sizeof(AVChapter)*os->nb_chapters);
|
||
1837 | if (!os->chapters)
|
||
1838 | return AVERROR(ENOMEM);
|
||
1839 | os->chapters[os->nb_chapters - 1] = out_ch;
|
||
1840 | } |
||
1841 | return 0; |
||
1842 | } |
||
1843 | |||
1844 | 4ad08021 | Nicolas George | static void parse_forced_key_frames(char *kf, AVOutputStream *ost, |
1845 | AVCodecContext *avctx) |
||
1846 | { |
||
1847 | char *p;
|
||
1848 | int n = 1, i; |
||
1849 | int64_t t; |
||
1850 | |||
1851 | for (p = kf; *p; p++)
|
||
1852 | if (*p == ',') |
||
1853 | n++; |
||
1854 | ost->forced_kf_count = n; |
||
1855 | ost->forced_kf_pts = av_malloc(sizeof(*ost->forced_kf_pts) * n);
|
||
1856 | if (!ost->forced_kf_pts) {
|
||
1857 | av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n"); |
||
1858 | ffmpeg_exit(1);
|
||
1859 | } |
||
1860 | for (i = 0; i < n; i++) { |
||
1861 | p = i ? strchr(p, ',') + 1 : kf; |
||
1862 | t = parse_time_or_die("force_key_frames", p, 1); |
||
1863 | ost->forced_kf_pts[i] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base); |
||
1864 | } |
||
1865 | } |
||
1866 | |||
1867 | 85f07f22 | Fabrice Bellard | /*
|
1868 | * The following code is the main loop of the file converter
|
||
1869 | */
|
||
1870 | 3aace1bc | Stefano Sabatini | static int transcode(AVFormatContext **output_files, |
1871 | 065a20cb | Stefano Sabatini | int nb_output_files,
|
1872 | AVFormatContext **input_files, |
||
1873 | int nb_input_files,
|
||
1874 | AVStreamMap *stream_maps, int nb_stream_maps)
|
||
1875 | 85f07f22 | Fabrice Bellard | { |
1876 | 002c95d7 | Baptiste Coudurier | int ret = 0, i, j, k, n, nb_istreams = 0, nb_ostreams = 0; |
1877 | 85f07f22 | Fabrice Bellard | AVFormatContext *is, *os; |
1878 | AVCodecContext *codec, *icodec; |
||
1879 | AVOutputStream *ost, **ost_table = NULL;
|
||
1880 | AVInputStream *ist, **ist_table = NULL;
|
||
1881 | bdc4796f | Fabrice Bellard | AVInputFile *file_table; |
1882 | 002c95d7 | Baptiste Coudurier | char error[1024]; |
1883 | 6d1ba1ac | Luca Abeni | int want_sdp = 1; |
1884 | 545465ec | Michael Niedermayer | uint8_t no_packet[MAX_FILES]={0};
|
1885 | int no_packet_count=0; |
||
1886 | bdc4796f | Fabrice Bellard | |
1887 | 90901860 | Michael Niedermayer | file_table= av_mallocz(nb_input_files * sizeof(AVInputFile));
|
1888 | bdc4796f | Fabrice Bellard | if (!file_table)
|
1889 | goto fail;
|
||
1890 | 115329f1 | Diego Biurrun | |
1891 | 85f07f22 | Fabrice Bellard | /* input stream init */
|
1892 | j = 0;
|
||
1893 | for(i=0;i<nb_input_files;i++) { |
||
1894 | is = input_files[i]; |
||
1895 | file_table[i].ist_index = j; |
||
1896 | 79fdaa4c | Fabrice Bellard | file_table[i].nb_streams = is->nb_streams; |
1897 | 85f07f22 | Fabrice Bellard | j += is->nb_streams; |
1898 | } |
||
1899 | nb_istreams = j; |
||
1900 | |||
1901 | ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
|
||
1902 | if (!ist_table)
|
||
1903 | bdc4796f | Fabrice Bellard | goto fail;
|
1904 | 115329f1 | Diego Biurrun | |
1905 | 85f07f22 | Fabrice Bellard | for(i=0;i<nb_istreams;i++) { |
1906 | ist = av_mallocz(sizeof(AVInputStream));
|
||
1907 | if (!ist)
|
||
1908 | goto fail;
|
||
1909 | ist_table[i] = ist; |
||
1910 | } |
||
1911 | j = 0;
|
||
1912 | for(i=0;i<nb_input_files;i++) { |
||
1913 | is = input_files[i]; |
||
1914 | for(k=0;k<is->nb_streams;k++) { |
||
1915 | ist = ist_table[j++]; |
||
1916 | ist->st = is->streams[k]; |
||
1917 | ist->file_index = i; |
||
1918 | ist->index = k; |
||
1919 | ist->discard = 1; /* the stream is discarded by default |
||
1920 | (changed later) */
|
||
1921 | bdfcbbed | Max Krasnyansky | |
1922 | 3a25ca18 | Stefano Sabatini | if (rate_emu) {
|
1923 | bdfcbbed | Max Krasnyansky | ist->start = av_gettime(); |
1924 | } |
||
1925 | 85f07f22 | Fabrice Bellard | } |
1926 | } |
||
1927 | |||
1928 | /* output stream init */
|
||
1929 | nb_ostreams = 0;
|
||
1930 | for(i=0;i<nb_output_files;i++) { |
||
1931 | os = output_files[i]; |
||
1932 | bb62d5c1 | Anton Khirnov | if (!os->nb_streams && !(os->oformat->flags & AVFMT_NOSTREAMS)) {
|
1933 | 610219a5 | Anton Khirnov | av_dump_format(output_files[i], i, output_files[i]->filename, 1);
|
1934 | fc5d0db5 | Stefano Sabatini | fprintf(stderr, "Output file #%d does not contain any stream\n", i);
|
1935 | d62ccec8 | Jai Menon | ret = AVERROR(EINVAL); |
1936 | goto fail;
|
||
1937 | 8a7bde1c | Baptiste Coudurier | } |
1938 | 85f07f22 | Fabrice Bellard | nb_ostreams += os->nb_streams; |
1939 | } |
||
1940 | if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) { |
||
1941 | fprintf(stderr, "Number of stream maps must match number of output streams\n");
|
||
1942 | d62ccec8 | Jai Menon | ret = AVERROR(EINVAL); |
1943 | goto fail;
|
||
1944 | 85f07f22 | Fabrice Bellard | } |
1945 | |||
1946 | bd073980 | Brian Foley | /* Sanity check the mapping args -- do the input files & streams exist? */
|
1947 | for(i=0;i<nb_stream_maps;i++) { |
||
1948 | int fi = stream_maps[i].file_index;
|
||
1949 | int si = stream_maps[i].stream_index;
|
||
1950 | 115329f1 | Diego Biurrun | |
1951 | bd073980 | Brian Foley | if (fi < 0 || fi > nb_input_files - 1 || |
1952 | si < 0 || si > file_table[fi].nb_streams - 1) { |
||
1953 | fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si);
|
||
1954 | d62ccec8 | Jai Menon | ret = AVERROR(EINVAL); |
1955 | goto fail;
|
||
1956 | bd073980 | Brian Foley | } |
1957 | b4a3389e | Wolfram Gloger | fi = stream_maps[i].sync_file_index; |
1958 | si = stream_maps[i].sync_stream_index; |
||
1959 | if (fi < 0 || fi > nb_input_files - 1 || |
||
1960 | si < 0 || si > file_table[fi].nb_streams - 1) { |
||
1961 | fprintf(stderr,"Could not find sync stream #%d.%d\n", fi, si);
|
||
1962 | d62ccec8 | Jai Menon | ret = AVERROR(EINVAL); |
1963 | goto fail;
|
||
1964 | b4a3389e | Wolfram Gloger | } |
1965 | bd073980 | Brian Foley | } |
1966 | 115329f1 | Diego Biurrun | |
1967 | 85f07f22 | Fabrice Bellard | ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
|
1968 | if (!ost_table)
|
||
1969 | goto fail;
|
||
1970 | n = 0;
|
||
1971 | for(k=0;k<nb_output_files;k++) { |
||
1972 | os = output_files[k]; |
||
1973 | de427ff4 | Stefano Sabatini | for(i=0;i<os->nb_streams;i++,n++) { |
1974 | 85f07f22 | Fabrice Bellard | int found;
|
1975 | 9fdf4b58 | Nicolas George | ost = ost_table[n] = output_streams_for_file[k][i]; |
1976 | 85f07f22 | Fabrice Bellard | ost->st = os->streams[i]; |
1977 | if (nb_stream_maps > 0) { |
||
1978 | de427ff4 | Stefano Sabatini | ost->source_index = file_table[stream_maps[n].file_index].ist_index + |
1979 | stream_maps[n].stream_index; |
||
1980 | 115329f1 | Diego Biurrun | |
1981 | bd073980 | Brian Foley | /* Sanity check that the stream types match */
|
1982 | 01f4895c | Michael Niedermayer | if (ist_table[ost->source_index]->st->codec->codec_type != ost->st->codec->codec_type) {
|
1983 | 150d5a25 | Stefano Sabatini | int i= ost->file_index;
|
1984 | 610219a5 | Anton Khirnov | av_dump_format(output_files[i], i, output_files[i]->filename, 1);
|
1985 | bd073980 | Brian Foley | fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n",
|
1986 | de427ff4 | Stefano Sabatini | stream_maps[n].file_index, stream_maps[n].stream_index, |
1987 | bd073980 | Brian Foley | ost->file_index, ost->index); |
1988 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
1989 | bd073980 | Brian Foley | } |
1990 | 115329f1 | Diego Biurrun | |
1991 | 85f07f22 | Fabrice Bellard | } else {
|
1992 | 247e3954 | Michael Niedermayer | int best_nb_frames=-1; |
1993 | 3f1710e7 | Ramiro Polla | /* get corresponding input stream index : we select the first one with the right type */
|
1994 | found = 0;
|
||
1995 | for(j=0;j<nb_istreams;j++) { |
||
1996 | int skip=0; |
||
1997 | ist = ist_table[j]; |
||
1998 | if(opt_programid){
|
||
1999 | int pi,si;
|
||
2000 | AVFormatContext *f= input_files[ ist->file_index ]; |
||
2001 | skip=1;
|
||
2002 | for(pi=0; pi<f->nb_programs; pi++){ |
||
2003 | AVProgram *p= f->programs[pi]; |
||
2004 | if(p->id == opt_programid)
|
||
2005 | for(si=0; si<p->nb_stream_indexes; si++){ |
||
2006 | if(f->streams[ p->stream_index[si] ] == ist->st)
|
||
2007 | skip=0;
|
||
2008 | } |
||
2009 | 6d3d3b83 | Michael Niedermayer | } |
2010 | 3f1710e7 | Ramiro Polla | } |
2011 | if (ist->discard && ist->st->discard != AVDISCARD_ALL && !skip &&
|
||
2012 | ist->st->codec->codec_type == ost->st->codec->codec_type) { |
||
2013 | if(best_nb_frames < ist->st->codec_info_nb_frames){
|
||
2014 | best_nb_frames= ist->st->codec_info_nb_frames; |
||
2015 | ost->source_index = j; |
||
2016 | found = 1;
|
||
2017 | 85f07f22 | Fabrice Bellard | } |
2018 | } |
||
2019 | 3f1710e7 | Ramiro Polla | } |
2020 | a15bc651 | Nico Sabbi | |
2021 | if (!found) {
|
||
2022 | if(! opt_programid) {
|
||
2023 | /* try again and reuse existing stream */
|
||
2024 | for(j=0;j<nb_istreams;j++) { |
||
2025 | ist = ist_table[j]; |
||
2026 | 6d3d3b83 | Michael Niedermayer | if ( ist->st->codec->codec_type == ost->st->codec->codec_type
|
2027 | && ist->st->discard != AVDISCARD_ALL) { |
||
2028 | a15bc651 | Nico Sabbi | ost->source_index = j; |
2029 | found = 1;
|
||
2030 | } |
||
2031 | } |
||
2032 | 50e143c4 | Nico Sabbi | } |
2033 | 85f07f22 | Fabrice Bellard | if (!found) {
|
2034 | 462cca10 | Stefano Sabatini | int i= ost->file_index;
|
2035 | 610219a5 | Anton Khirnov | av_dump_format(output_files[i], i, output_files[i]->filename, 1);
|
2036 | 85f07f22 | Fabrice Bellard | fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
|
2037 | ost->file_index, ost->index); |
||
2038 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
2039 | 85f07f22 | Fabrice Bellard | } |
2040 | } |
||
2041 | } |
||
2042 | ist = ist_table[ost->source_index]; |
||
2043 | ist->discard = 0;
|
||
2044 | b4a3389e | Wolfram Gloger | ost->sync_ist = (nb_stream_maps > 0) ?
|
2045 | de427ff4 | Stefano Sabatini | ist_table[file_table[stream_maps[n].sync_file_index].ist_index + |
2046 | stream_maps[n].sync_stream_index] : ist; |
||
2047 | 85f07f22 | Fabrice Bellard | } |
2048 | } |
||
2049 | |||
2050 | /* for each output stream, we compute the right encoding parameters */
|
||
2051 | for(i=0;i<nb_ostreams;i++) { |
||
2052 | ost = ost_table[i]; |
||
2053 | 365515ac | Michael Niedermayer | os = output_files[ost->file_index]; |
2054 | 85f07f22 | Fabrice Bellard | ist = ist_table[ost->source_index]; |
2055 | |||
2056 | 01f4895c | Michael Niedermayer | codec = ost->st->codec; |
2057 | icodec = ist->st->codec; |
||
2058 | 85f07f22 | Fabrice Bellard | |
2059 | d0abe80a | Anton Khirnov | if (metadata_streams_autocopy)
|
2060 | 7f88a5bf | Ronald S. Bultje | av_metadata_copy(&ost->st->metadata, ist->st->metadata, |
2061 | AV_METADATA_DONT_OVERWRITE); |
||
2062 | 8e0d882b | Aurelien Jacobs | |
2063 | 90c2295b | Evgeniy Stepanov | ost->st->disposition = ist->st->disposition; |
2064 | a39b76ea | Michael Niedermayer | codec->bits_per_raw_sample= icodec->bits_per_raw_sample; |
2065 | de961801 | David Conrad | codec->chroma_sample_location = icodec->chroma_sample_location; |
2066 | 90c2295b | Evgeniy Stepanov | |
2067 | 1629626f | Fabrice Bellard | if (ost->st->stream_copy) {
|
2068 | b659c8b4 | Luca Abeni | uint64_t extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE; |
2069 | |||
2070 | if (extra_size > INT_MAX)
|
||
2071 | goto fail;
|
||
2072 | |||
2073 | 1629626f | Fabrice Bellard | /* if stream_copy is selected, no need to decode or encode */
|
2074 | codec->codec_id = icodec->codec_id; |
||
2075 | codec->codec_type = icodec->codec_type; |
||
2076 | 365515ac | Michael Niedermayer | |
2077 | if(!codec->codec_tag){
|
||
2078 | if( !os->oformat->codec_tag
|
||
2079 | 37ce3d6b | Michael Niedermayer | || av_codec_get_id (os->oformat->codec_tag, icodec->codec_tag) == codec->codec_id |
2080 | 365515ac | Michael Niedermayer | || av_codec_get_tag(os->oformat->codec_tag, icodec->codec_id) <= 0)
|
2081 | codec->codec_tag = icodec->codec_tag; |
||
2082 | } |
||
2083 | |||
2084 | 1629626f | Fabrice Bellard | codec->bit_rate = icodec->bit_rate; |
2085 | 2dec2bb8 | Michael Niedermayer | codec->rc_max_rate = icodec->rc_max_rate; |
2086 | codec->rc_buffer_size = icodec->rc_buffer_size; |
||
2087 | b659c8b4 | Luca Abeni | codec->extradata= av_mallocz(extra_size); |
2088 | if (!codec->extradata)
|
||
2089 | goto fail;
|
||
2090 | memcpy(codec->extradata, icodec->extradata, icodec->extradata_size); |
||
2091 | dffbfd06 | Michael Niedermayer | codec->extradata_size= icodec->extradata_size; |
2092 | 59e2118e | Michael Niedermayer | 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){ |
2093 | d29de719 | Michael Niedermayer | codec->time_base = icodec->time_base; |
2094 | 3797c74b | Michael Niedermayer | codec->time_base.num *= icodec->ticks_per_frame; |
2095 | 8abcbf2d | Baptiste Coudurier | av_reduce(&codec->time_base.num, &codec->time_base.den, |
2096 | codec->time_base.num, codec->time_base.den, INT_MAX); |
||
2097 | 3797c74b | Michael Niedermayer | }else
|
2098 | d29de719 | Michael Niedermayer | codec->time_base = ist->st->time_base; |
2099 | 1629626f | Fabrice Bellard | switch(codec->codec_type) {
|
2100 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_AUDIO:
|
2101 | d08e3e91 | Ramiro Polla | if(audio_volume != 256) { |
2102 | fprintf(stderr,"-acodec copy and -vol are incompatible (frames are not decoded)\n");
|
||
2103 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
2104 | d08e3e91 | Ramiro Polla | } |
2105 | 13367a46 | Benjamin Larsson | codec->channel_layout = icodec->channel_layout; |
2106 | 1629626f | Fabrice Bellard | codec->sample_rate = icodec->sample_rate; |
2107 | codec->channels = icodec->channels; |
||
2108 | 0a3b0447 | Michael Niedermayer | codec->frame_size = icodec->frame_size; |
2109 | 34b47d7c | Justin Ruggles | codec->audio_service_type = icodec->audio_service_type; |
2110 | c1344911 | Michael Niedermayer | codec->block_align= icodec->block_align; |
2111 | 4efd6f58 | Michael Niedermayer | if(codec->block_align == 1 && codec->codec_id == CODEC_ID_MP3) |
2112 | codec->block_align= 0;
|
||
2113 | bcbd328e | Michael Niedermayer | if(codec->codec_id == CODEC_ID_AC3)
|
2114 | codec->block_align= 0;
|
||
2115 | 1629626f | Fabrice Bellard | break;
|
2116 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_VIDEO:
|
2117 | 9df3437f | Baptiste Coudurier | codec->pix_fmt = icodec->pix_fmt; |
2118 | 1629626f | Fabrice Bellard | codec->width = icodec->width; |
2119 | codec->height = icodec->height; |
||
2120 | ff8cc24b | Michael Niedermayer | codec->has_b_frames = icodec->has_b_frames; |
2121 | 1629626f | Fabrice Bellard | break;
|
2122 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_SUBTITLE:
|
2123 | d43b26ea | David Conrad | codec->width = icodec->width; |
2124 | codec->height = icodec->height; |
||
2125 | cf7fc795 | Fabrice Bellard | break;
|
2126 | e3b540b4 | Luca Barbato | case AVMEDIA_TYPE_DATA:
|
2127 | break;
|
||
2128 | 1629626f | Fabrice Bellard | default:
|
2129 | 0f4e8165 | Ronald S. Bultje | abort(); |
2130 | 1629626f | Fabrice Bellard | } |
2131 | } else {
|
||
2132 | switch(codec->codec_type) {
|
||
2133 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_AUDIO:
|
2134 | 41dd680d | Michael Niedermayer | ost->fifo= av_fifo_alloc(1024);
|
2135 | if(!ost->fifo)
|
||
2136 | 85f07f22 | Fabrice Bellard | goto fail;
|
2137 | 5d6e4c16 | Stefano Sabatini | ost->reformat_pair = MAKE_SFMT_PAIR(AV_SAMPLE_FMT_NONE,AV_SAMPLE_FMT_NONE); |
2138 | 2886f311 | Andreas Öman | ost->audio_resample = codec->sample_rate != icodec->sample_rate || audio_sync_method > 1;
|
2139 | icodec->request_channels = codec->channels; |
||
2140 | 85f07f22 | Fabrice Bellard | ist->decoding_needed = 1;
|
2141 | ost->encoding_needed = 1;
|
||
2142 | 8afab686 | Stefano Sabatini | ost->resample_sample_fmt = icodec->sample_fmt; |
2143 | ost->resample_sample_rate = icodec->sample_rate; |
||
2144 | ost->resample_channels = icodec->channels; |
||
2145 | 1629626f | Fabrice Bellard | break;
|
2146 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_VIDEO:
|
2147 | 761c8c92 | Baptiste Coudurier | if (ost->st->codec->pix_fmt == PIX_FMT_NONE) {
|
2148 | 562f22a6 | Ronald S. Bultje | fprintf(stderr, "Video pixel format is unknown, stream cannot be encoded\n");
|
2149 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
2150 | 761c8c92 | Baptiste Coudurier | } |
2151 | 2b95602e | Stefano Sabatini | ost->video_resample = codec->width != icodec->width || |
2152 | codec->height != icodec->height || |
||
2153 | codec->pix_fmt != icodec->pix_fmt; |
||
2154 | c3f11d19 | Luca Abeni | if (ost->video_resample) {
|
2155 | 8a774d3d | Baptiste Coudurier | #if !CONFIG_AVFILTER
|
2156 | 2de28abb | Michael Niedermayer | avcodec_get_frame_defaults(&ost->pict_tmp); |
2157 | 9d58e0a9 | Baptiste Coudurier | if(avpicture_alloc((AVPicture*)&ost->pict_tmp, codec->pix_fmt,
|
2158 | codec->width, codec->height)) { |
||
2159 | eddc482d | Baptiste Coudurier | fprintf(stderr, "Cannot allocate temp picture, check pix fmt\n");
|
2160 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
2161 | eddc482d | Baptiste Coudurier | } |
2162 | 18a54b04 | Luca Abeni | ost->img_resample_ctx = sws_getContext( |
2163 | 5879ea6d | Stefano Sabatini | icodec->width, |
2164 | icodec->height, |
||
2165 | 18a54b04 | Luca Abeni | icodec->pix_fmt, |
2166 | 0c22311b | Michael Niedermayer | codec->width, |
2167 | codec->height, |
||
2168 | 18a54b04 | Luca Abeni | codec->pix_fmt, |
2169 | 1435f2fa | Anton Khirnov | ost->sws_flags, NULL, NULL, NULL); |
2170 | 0b50ac8a | Luca Abeni | if (ost->img_resample_ctx == NULL) { |
2171 | fprintf(stderr, "Cannot get resampling context\n");
|
||
2172 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
2173 | 0b50ac8a | Luca Abeni | } |
2174 | 46847a33 | Michael Niedermayer | #endif
|
2175 | a39b76ea | Michael Niedermayer | codec->bits_per_raw_sample= 0;
|
2176 | 85f07f22 | Fabrice Bellard | } |
2177 | 5879ea6d | Stefano Sabatini | ost->resample_height = icodec->height; |
2178 | ost->resample_width = icodec->width; |
||
2179 | b83ccbff | Michael Niedermayer | ost->resample_pix_fmt= icodec->pix_fmt; |
2180 | 85f07f22 | Fabrice Bellard | ost->encoding_needed = 1;
|
2181 | ist->decoding_needed = 1;
|
||
2182 | 46847a33 | Michael Niedermayer | |
2183 | #if CONFIG_AVFILTER
|
||
2184 | 0420bf09 | Stefano Sabatini | if (configure_video_filters(ist, ost)) {
|
2185 | 46847a33 | Michael Niedermayer | fprintf(stderr, "Error opening filters!\n");
|
2186 | exit(1);
|
||
2187 | } |
||
2188 | #endif
|
||
2189 | 1629626f | Fabrice Bellard | break;
|
2190 | 72415b2a | Stefano Sabatini | case AVMEDIA_TYPE_SUBTITLE:
|
2191 | cf7fc795 | Fabrice Bellard | ost->encoding_needed = 1;
|
2192 | ist->decoding_needed = 1;
|
||
2193 | break;
|
||
2194 | 1629626f | Fabrice Bellard | default:
|
2195 | 0f4e8165 | Ronald S. Bultje | abort(); |
2196 | cf7fc795 | Fabrice Bellard | break;
|
2197 | 85f07f22 | Fabrice Bellard | } |
2198 | 1629626f | Fabrice Bellard | /* two pass mode */
|
2199 | 115329f1 | Diego Biurrun | if (ost->encoding_needed &&
|
2200 | 1629626f | Fabrice Bellard | (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) { |
2201 | char logfilename[1024]; |
||
2202 | FILE *f; |
||
2203 | 115329f1 | Diego Biurrun | |
2204 | snprintf(logfilename, sizeof(logfilename), "%s-%d.log", |
||
2205 | 22730e87 | Stefano Sabatini | pass_logfilename_prefix ? pass_logfilename_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX, |
2206 | i); |
||
2207 | 1629626f | Fabrice Bellard | if (codec->flags & CODEC_FLAG_PASS1) {
|
2208 | c56e9e05 | Ramiro Polla | f = fopen(logfilename, "wb");
|
2209 | 1629626f | Fabrice Bellard | if (!f) {
|
2210 | 42d1d06e | Stefano Sabatini | fprintf(stderr, "Cannot write log file '%s' for pass-1 encoding: %s\n", logfilename, strerror(errno));
|
2211 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
2212 | 1629626f | Fabrice Bellard | } |
2213 | ost->logfile = f; |
||
2214 | } else {
|
||
2215 | 458b062d | Stefano Sabatini | char *logbuffer;
|
2216 | size_t logbuffer_size; |
||
2217 | if (read_file(logfilename, &logbuffer, &logbuffer_size) < 0) { |
||
2218 | fprintf(stderr, "Error reading log file '%s' for pass-2 encoding\n", logfilename);
|
||
2219 | 639e4ec8 | Stefano Sabatini | ffmpeg_exit(1);
|
2220 | 1629626f | Fabrice Bellard | } |
2221 | codec->stats_in = logbuffer; |
||
2222 | 5abdb4b1 | Fabrice Bellard | } |
2223 | } |
||
2224 | } |
||
2225 | 72415b2a | Stefano Sabatini | if(codec->codec_type == AVMEDIA_TYPE_VIDEO){
|
2226 | 8a6cb114 | Michael Niedermayer | int size= codec->width * codec->height;
|
2227 | c027e91a | Peter Ross | bit_buffer_size= FFMAX(bit_buffer_size, 6*size + 200); |
2228 | 8a6cb114 | Michael Niedermayer | } |
2229 | 85f07f22 | Fabrice Bellard | } |
2230 | |||
2231 | 8a6cb114 | Michael Niedermayer | if (!bit_buffer)
|
2232 | bit_buffer = av_malloc(bit_buffer_size); |
||
2233 | 002c95d7 | Baptiste Coudurier | if (!bit_buffer) {
|
2234 | 50f3fabc | Måns Rullgård | fprintf(stderr, "Cannot allocate %d bytes output buffer\n",
|
2235 | bit_buffer_size); |
||
2236 | 002c95d7 | Baptiste Coudurier | ret = AVERROR(ENOMEM); |
2237 | 8a6cb114 | Michael Niedermayer | goto fail;
|
2238 | 1629626f | Fabrice Bellard | } |
2239 | |||
2240 | 85f07f22 | Fabrice Bellard | /* open each encoder */
|
2241 | for(i=0;i<nb_ostreams;i++) { |
||
2242 | ost = ost_table[i]; |
||
2243 | if (ost->encoding_needed) {
|
||
2244 | 0a6d97b3 | Aurelien Jacobs | AVCodec *codec = i < nb_output_codecs ? output_codecs[i] : NULL;
|
2245 | cb2c971d | Aurelien Jacobs | AVCodecContext *dec = ist_table[ost->source_index]->st->codec; |
2246 | 6488cf9b | Aurelien Jacobs | if (!codec)
|
2247 | fd2b356a | Aurelien Jacobs | codec = avcodec_find_encoder(ost->st->codec->codec_id); |
2248 | 85f07f22 | Fabrice Bellard | if (!codec) {
|
2249 | f356fc57 | Baptiste Coudurier | snprintf(error, sizeof(error), "Encoder (codec id %d) not found for output stream #%d.%d", |
2250 | ost->st->codec->codec_id, ost->file_index, ost->index); |
||
2251 | 002c95d7 | Baptiste Coudurier | ret = AVERROR(EINVAL); |
2252 | goto dump_format;
|
||
2253 | 85f07f22 | Fabrice Bellard | } |
2254 | cb2c971d | Aurelien Jacobs | if (dec->subtitle_header) {
|
2255 | ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size); |
||
2256 | if (!ost->st->codec->subtitle_header) {
|
||
2257 | ret = AVERROR(ENOMEM); |
||
2258 | goto dump_format;
|
||
2259 | } |
||
2260 | memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size); |
||
2261 | ost->st->codec->subtitle_header_size = dec->subtitle_header_size; |
||
2262 | } |
||
2263 | 01f4895c | Michael Niedermayer | if (avcodec_open(ost->st->codec, codec) < 0) { |
2264 | f356fc57 | Baptiste Coudurier | snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height", |
2265 | 85f07f22 | Fabrice Bellard | ost->file_index, ost->index); |
2266 | 002c95d7 | Baptiste Coudurier | ret = AVERROR(EINVAL); |
2267 | goto dump_format;
|
||
2268 | 85f07f22 | Fabrice Bellard | } |
2269 | 01f4895c | Michael Niedermayer | extra_size += ost->st->codec->extradata_size; |
2270 | 85f07f22 | Fabrice Bellard | } |
2271 | } |
||
2272 | |||
2273 | /* open each decoder */
|
||
2274 | for(i=0;i<nb_istreams;i++) { |
||
2275 | ist = ist_table[i]; |
||
2276 | if (ist->decoding_needed) {
|
||
2277 | 311e223f | Aurelien Jacobs | AVCodec *codec = i < nb_input_codecs ? input_codecs[i] : NULL;
|
2278 | 6488cf9b | Aurelien Jacobs | if (!codec)
|
2279 | fd2b356a | Aurelien Jacobs | codec = avcodec_find_decoder(ist->st->codec->codec_id); |
2280 | 85f07f22 | Fabrice Bellard | if (!codec) {
|
2281 | f356fc57 | Baptiste Coudurier | snprintf(error, sizeof(error), "Decoder (codec id %d) not found for input stream #%d.%d", |
2282 | 01f4895c | Michael Niedermayer | ist->st->codec->codec_id, ist->file_index, ist->index); |
2283 | 002c95d7 | Baptiste Coudurier | ret = AVERROR(EINVAL); |
2284 | goto dump_format;
|
||
2285 | 85f07f22 | Fabrice Bellard | } |
2286 | 01f4895c | Michael Niedermayer | if (avcodec_open(ist->st->codec, codec) < 0) { |
2287 | f356fc57 | Baptiste Coudurier | snprintf(error, sizeof(error), "Error while opening decoder for input stream #%d.%d", |
2288 | 85f07f22 | Fabrice Bellard | ist->file_index, ist->index); |
2289 | 002c95d7 | Baptiste Coudurier | ret = AVERROR(EINVAL); |
2290 | goto dump_format;
|
||
2291 | 85f07f22 | Fabrice Bellard | } |
2292 | 72415b2a | Stefano Sabatini | //if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
|
2293 | 01f4895c | Michael Niedermayer | // ist->st->codec->flags |= CODEC_FLAG_REPEAT_FIELD;
|
2294 | 85f07f22 | Fabrice Bellard | } |
2295 | } |
||
2296 | |||
2297 | /* init pts */
|
||
2298 | for(i=0;i<nb_istreams;i++) { |
||
2299 | b8c93c48 | Michael Niedermayer | AVStream *st; |
2300 | 85f07f22 | Fabrice Bellard | ist = ist_table[i]; |
2301 | b8c93c48 | Michael Niedermayer | st= ist->st; |
2302 | ist->pts = st->avg_frame_rate.num ? - st->codec->has_b_frames*AV_TIME_BASE / av_q2d(st->avg_frame_rate) : 0;
|
||
2303 | 48291040 | Michael Niedermayer | ist->next_pts = AV_NOPTS_VALUE; |
2304 | 0ff4f0c0 | Alexander Strange | init_pts_correction(&ist->pts_ctx); |
2305 | ff4905a5 | Michael Niedermayer | ist->is_start = 1;
|
2306 | 85f07f22 | Fabrice Bellard | } |
2307 | c5e1e982 | Justin Johnson | |
2308 | 0a38bafd | Patrice Bensoussan | /* set meta data information from input file if required */
|
2309 | for (i=0;i<nb_meta_data_maps;i++) { |
||
2310 | 1829e195 | Anton Khirnov | AVFormatContext *files[2];
|
2311 | AVMetadata **meta[2];
|
||
2312 | int j;
|
||
2313 | 0a38bafd | Patrice Bensoussan | |
2314 | 1829e195 | Anton Khirnov | #define METADATA_CHECK_INDEX(index, nb_elems, desc)\
|
2315 | if ((index) < 0 || (index) >= (nb_elems)) {\ |
||
2316 | snprintf(error, sizeof(error), "Invalid %s index %d while processing metadata maps\n",\ |
||
2317 | (desc), (index));\ |
||
2318 | ret = AVERROR(EINVAL);\ |
||
2319 | goto dump_format;\
|
||
2320 | 115329f1 | Diego Biurrun | } |
2321 | |||
2322 | 1829e195 | Anton Khirnov | int out_file_index = meta_data_maps[i][0].file; |
2323 | int in_file_index = meta_data_maps[i][1].file; |
||
2324 | 7b393736 | Anton Khirnov | if (in_file_index < 0 || out_file_index < 0) |
2325 | continue;
|
||
2326 | 1829e195 | Anton Khirnov | METADATA_CHECK_INDEX(out_file_index, nb_output_files, "output file")
|
2327 | METADATA_CHECK_INDEX(in_file_index, nb_input_files, "input file")
|
||
2328 | 0a38bafd | Patrice Bensoussan | |
2329 | 1829e195 | Anton Khirnov | files[0] = output_files[out_file_index];
|
2330 | files[1] = input_files[in_file_index];
|
||
2331 | |||
2332 | for (j = 0; j < 2; j++) { |
||
2333 | AVMetaDataMap *map = &meta_data_maps[i][j]; |
||
2334 | |||
2335 | switch (map->type) {
|
||
2336 | case 'g': |
||
2337 | meta[j] = &files[j]->metadata; |
||
2338 | break;
|
||
2339 | case 's': |
||
2340 | METADATA_CHECK_INDEX(map->index, files[j]->nb_streams, "stream")
|
||
2341 | meta[j] = &files[j]->streams[map->index]->metadata; |
||
2342 | break;
|
||
2343 | case 'c': |
||
2344 | METADATA_CHECK_INDEX(map->index, files[j]->nb_chapters, "chapter")
|
||
2345 | meta[j] = &files[j]->chapters[map->index]->metadata; |
||
2346 | break;
|
||
2347 | case 'p': |
||
2348 | METADATA_CHECK_INDEX(map->index, files[j]->nb_programs, "program")
|
||
2349 | meta[j] = &files[j]->programs[map->index]->metadata; |
||
2350 | break;
|
||
2351 | } |
||
2352 | } |
||
2353 | a5926d85 | Aurelien Jacobs | |
2354 | 7f88a5bf | Ronald S. Bultje | av_metadata_copy(meta[0], *meta[1], AV_METADATA_DONT_OVERWRITE); |
2355 | 0a38bafd | Patrice Bensoussan | } |
2356 | 115329f1 | Diego Biurrun | |
2357 | 477b1aea | Anton Khirnov | /* copy global metadata by default */
|
2358 | if (metadata_global_autocopy) {
|
||
2359 | |||
2360 | 8e8a3cc2 | Ronald S. Bultje | for (i = 0; i < nb_output_files; i++) |
2361 | 7f88a5bf | Ronald S. Bultje | av_metadata_copy(&output_files[i]->metadata, input_files[0]->metadata,
|
2362 | AV_METADATA_DONT_OVERWRITE); |
||
2363 | 477b1aea | Anton Khirnov | } |
2364 | |||
2365 | 91e96eba | Anton Khirnov | /* copy chapters according to chapter maps */
|
2366 | for (i = 0; i < nb_chapter_maps; i++) { |
||
2367 | int infile = chapter_maps[i].in_file;
|
||
2368 | int outfile = chapter_maps[i].out_file;
|
||
2369 | |||
2370 | if (infile < 0 || outfile < 0) |
||
2371 | continue;
|
||
2372 | if (infile >= nb_input_files) {
|
||
2373 | snprintf(error, sizeof(error), "Invalid input file index %d in chapter mapping.\n", infile); |
||
2374 | ret = AVERROR(EINVAL); |
||
2375 | goto dump_format;
|
||
2376 | } |
||
2377 | if (outfile >= nb_output_files) {
|
||
2378 | snprintf(error, sizeof(error), "Invalid output file index %d in chapter mapping.\n",outfile); |
||
2379 | ret = AVERROR(EINVAL); |
||
2380 | goto dump_format;
|
||
2381 | } |
||
2382 | copy_chapters(infile, outfile); |
||
2383 | } |
||
2384 | |||
2385 | 7a39f142 | Anton Khirnov | /* copy chapters from the first input file that has them*/
|
2386 | 91e96eba | Anton Khirnov | if (!nb_chapter_maps)
|
2387 | a9c2bf9d | Anton Khirnov | for (i = 0; i < nb_input_files; i++) { |
2388 | if (!input_files[i]->nb_chapters)
|
||
2389 | continue;
|
||
2390 | 7a39f142 | Anton Khirnov | |
2391 | a9c2bf9d | Anton Khirnov | for (j = 0; j < nb_output_files; j++) |
2392 | if ((ret = copy_chapters(i, j)) < 0) |
||
2393 | goto dump_format;
|
||
2394 | 09f47fa4 | Anton Khirnov | break;
|
2395 | a9c2bf9d | Anton Khirnov | } |
2396 | 7a39f142 | Anton Khirnov | |
2397 | 85f07f22 | Fabrice Bellard | /* open files and write file headers */
|
2398 | for(i=0;i<nb_output_files;i++) { |
||
2399 | os = output_files[i]; |
||
2400 | 79fdaa4c | Fabrice Bellard | if (av_write_header(os) < 0) { |
2401 | 002c95d7 | Baptiste Coudurier | snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?)", i); |
2402 | 8fa36ae0 | François Revol | ret = AVERROR(EINVAL); |
2403 | 002c95d7 | Baptiste Coudurier | goto dump_format;
|
2404 | a38469e1 | Fabrice Bellard | } |
2405 | 6d1ba1ac | Luca Abeni | if (strcmp(output_files[i]->oformat->name, "rtp")) { |
2406 | want_sdp = 0;
|
||
2407 | } |
||
2408 | } |
||
2409 | 002c95d7 | Baptiste Coudurier | |
2410 | dump_format:
|
||
2411 | /* dump the file output parameters - cannot be done before in case
|
||
2412 | of stream copy */
|
||
2413 | for(i=0;i<nb_output_files;i++) { |
||
2414 | 610219a5 | Anton Khirnov | av_dump_format(output_files[i], i, output_files[i]->filename, 1);
|
2415 | 002c95d7 | Baptiste Coudurier | } |
2416 | |||
2417 | /* dump the stream mapping */
|
||
2418 | if (verbose >= 0) { |
||
2419 | fprintf(stderr, "Stream mapping:\n");
|
||
2420 | for(i=0;i<nb_ostreams;i++) { |
||
2421 | ost = ost_table[i]; |
||
2422 | fprintf(stderr, " Stream #%d.%d -> #%d.%d",
|
||
2423 | ist_table[ost->source_index]->file_index, |
||
2424 | ist_table[ost->source_index]->index, |
||
2425 | ost->file_index, |
||
2426 | ost->index); |
||
2427 | if (ost->sync_ist != ist_table[ost->source_index])
|
||
2428 | fprintf(stderr, " [sync #%d.%d]",
|
||
2429 | ost->sync_ist->file_index, |
||
2430 | ost->sync_ist->index); |
||
2431 | fprintf(stderr, "\n");
|
||
2432 | } |
||
2433 | } |
||
2434 | |||
2435 | if (ret) {
|
||
2436 | fprintf(stderr, "%s\n", error);
|
||
2437 | goto fail;
|
||
2438 | } |
||
2439 | |||
2440 | 6d1ba1ac | Luca Abeni | if (want_sdp) {
|
2441 | print_sdp(output_files, nb_output_files); |
||
2442 | 85f07f22 | Fabrice Bellard | } |
2443 | |||
2444 | 8fb566fd | Anton Khirnov | if (verbose >= 0) |
2445 | cb48e245 | Panagiotis H.M. Issaris | fprintf(stderr, "Press ctrl-c to stop encoding\n");
|
2446 | a38469e1 | Fabrice Bellard | term_init(); |
2447 | |||
2448 | 7fc98937 | Limin Wang | timer_start = av_gettime(); |
2449 | 51bd4565 | Philip Gladstone | |
2450 | d9a916e2 | Charles Yates | for(; received_sigterm == 0;) { |
2451 | 85f07f22 | Fabrice Bellard | int file_index, ist_index;
|
2452 | AVPacket pkt; |
||
2453 | a9aeda81 | Patrice Bensoussan | double ipts_min;
|
2454 | double opts_min;
|
||
2455 | 23ffe323 | Michael Niedermayer | |
2456 | 85f07f22 | Fabrice Bellard | redo:
|
2457 | a9aeda81 | Patrice Bensoussan | ipts_min= 1e100;
|
2458 | opts_min= 1e100;
|
||
2459 | a38469e1 | Fabrice Bellard | |
2460 | ec5517d5 | Fabrice Bellard | /* select the stream that we must read now by looking at the
|
2461 | smallest output pts */
|
||
2462 | 85f07f22 | Fabrice Bellard | file_index = -1;
|
2463 | ec5517d5 | Fabrice Bellard | for(i=0;i<nb_ostreams;i++) { |
2464 | 23ffe323 | Michael Niedermayer | double ipts, opts;
|
2465 | ec5517d5 | Fabrice Bellard | ost = ost_table[i]; |
2466 | os = output_files[ost->file_index]; |
||
2467 | ist = ist_table[ost->source_index]; |
||
2468 | 55a7e946 | Wolfram Gloger | if(ist->is_past_recording_time || no_packet[ist->file_index])
|
2469 | 545465ec | Michael Niedermayer | continue;
|
2470 | c0df9d75 | Michael Niedermayer | opts = ost->st->pts.val * av_q2d(ost->st->time_base); |
2471 | 23ffe323 | Michael Niedermayer | ipts = (double)ist->pts;
|
2472 | if (!file_table[ist->file_index].eof_reached){
|
||
2473 | if(ipts < ipts_min) {
|
||
2474 | ipts_min = ipts; |
||
2475 | if(input_sync ) file_index = ist->file_index;
|
||
2476 | } |
||
2477 | if(opts < opts_min) {
|
||
2478 | opts_min = opts; |
||
2479 | if(!input_sync) file_index = ist->file_index;
|
||
2480 | } |
||
2481 | 85f07f22 | Fabrice Bellard | } |
2482 | 01f4895c | Michael Niedermayer | if(ost->frame_number >= max_frames[ost->st->codec->codec_type]){
|
2483 | 7ca60994 | Michael Niedermayer | file_index= -1;
|
2484 | break;
|
||
2485 | } |
||
2486 | 85f07f22 | Fabrice Bellard | } |
2487 | /* if none, if is finished */
|
||
2488 | 51bd4565 | Philip Gladstone | if (file_index < 0) { |
2489 | 545465ec | Michael Niedermayer | if(no_packet_count){
|
2490 | no_packet_count=0;
|
||
2491 | memset(no_packet, 0, sizeof(no_packet)); |
||
2492 | d61f30a7 | Michael Niedermayer | usleep(10000);
|
2493 | 545465ec | Michael Niedermayer | continue;
|
2494 | } |
||
2495 | 85f07f22 | Fabrice Bellard | break;
|
2496 | ec5517d5 | Fabrice Bellard | } |
2497 | |||
2498 | b6e16b86 | Calcium | /* finish if limit size exhausted */
|
2499 | a2704c97 | Anton Khirnov | if (limit_filesize != 0 && limit_filesize <= avio_tell(output_files[0]->pb)) |
2500 | b6e16b86 | Calcium | break;
|
2501 | |||
2502 | 254abc2e | Fabrice Bellard | /* read a frame from it and output it in the fifo */
|
2503 | 85f07f22 | Fabrice Bellard | is = input_files[file_index]; |
2504 | ad51c68c | Michael Niedermayer | ret= av_read_frame(is, &pkt); |
2505 | b9edbe99 | Baptiste Coudurier | if(ret == AVERROR(EAGAIN)){
|
2506 | 545465ec | Michael Niedermayer | no_packet[file_index]=1;
|
2507 | no_packet_count++; |
||
2508 | ad51c68c | Michael Niedermayer | continue;
|
2509 | 545465ec | Michael Niedermayer | } |
2510 | ad51c68c | Michael Niedermayer | if (ret < 0) { |
2511 | 85f07f22 | Fabrice Bellard | file_table[file_index].eof_reached = 1;
|
2512 | d24ce947 | Alex Beregszaszi | if (opt_shortest)
|
2513 | break;
|
||
2514 | else
|
||
2515 | continue;
|
||
2516 | 85f07f22 | Fabrice Bellard | } |
2517 | 303e50e6 | Michael Niedermayer | |
2518 | 545465ec | Michael Niedermayer | no_packet_count=0;
|
2519 |