demux_mkv: read tags.
[mplayer.git] / mencoder.c
blobdc3d8700a399c42755a2b1c6dc2952f8e3116de8
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #define VCODEC_COPY 0
20 #define VCODEC_FRAMENO 1
21 // real codecs:
22 #define VCODEC_LIBAVCODEC 4
23 #define VCODEC_VFW 7
24 #define VCODEC_LIBDV 8
25 #define VCODEC_XVID 9
26 #define VCODEC_QTVIDEO 10
27 #define VCODEC_NUV 11
28 #define VCODEC_RAW 12
29 #define VCODEC_X264 13
31 #define ACODEC_COPY 0
32 #define ACODEC_PCM 1
33 #define ACODEC_VBRMP3 2
34 #define ACODEC_NULL 3
35 #define ACODEC_LAVC 4
36 #define ACODEC_TOOLAME 5
37 #define ACODEC_FAAC 6
38 #define ACODEC_TWOLAME 7
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <signal.h>
44 #include "config.h"
46 #ifdef __MINGW32__
47 #define SIGHUP 1
48 #define SIGQUIT 3
49 #define SIGPIPE 13
50 #endif
51 #if defined(__MINGW32__) || defined(__CYGWIN__)
52 #include <windows.h>
53 #endif
55 #include <sys/time.h>
57 #include "mp_msg.h"
58 #include "av_log.h"
60 #include "codec-cfg.h"
61 #include "m_option.h"
62 #include "m_config.h"
63 #include "parser-mecmd.h"
64 #include "parser-cfg.h"
65 #include "mp_fifo.h"
66 #include "path.h"
68 #include "stream/stream.h"
69 #include "libmpdemux/aviprint.h"
70 #include "libmpdemux/demuxer.h"
71 #include "libmpdemux/stheader.h"
72 #include "libmpdemux/mp3_hdr.h"
73 #include "libmpdemux/muxer.h"
75 #include "input/input.h"
76 #include "libvo/video_out.h"
78 #include "libaf/af_format.h"
80 #include "libmpcodecs/mp_image.h"
81 #include "libmpcodecs/dec_audio.h"
82 #include "libmpcodecs/dec_video.h"
83 #include "libmpcodecs/vf.h"
84 #include "libmpcodecs/vd.h"
86 // for MPEGLAYER3WAVEFORMAT:
87 #include "libmpdemux/ms_hdr.h"
89 #include <inttypes.h>
91 #include "libvo/fastmemcpy.h"
93 #include "osdep/timer.h"
95 #ifdef CONFIG_DVDREAD
96 #include "stream/stream_dvd.h"
97 #endif
99 #include "stream/stream_dvdnav.h"
100 #include "libavcodec/avcodec.h"
102 #include "libmpcodecs/ae.h"
103 #include "options.h"
104 #include "defaultopts.h"
106 MPOpts opts;
107 struct osd_state *osd;
109 const int under_mencoder = 1;
110 int vo_doublebuffering=0;
111 int vo_directrendering=0;
112 int forced_subs_only=0;
114 //--------------------------
116 // cache2:
117 int stream_cache_size=-1;
118 #ifdef CONFIG_STREAM_CACHE
119 extern int cache_fill_status;
121 float stream_cache_min_percent=20.0;
122 float stream_cache_seek_min_percent=50.0;
123 #else
124 #define cache_fill_status 0
125 #endif
127 int vobsub_id=-1;
128 char* audio_lang=NULL;
129 char* dvdsub_lang=NULL;
130 static char* spudec_ifo=NULL;
132 static char** audio_codec_list=NULL; // override audio codec
133 static char** video_codec_list=NULL; // override video codec
134 static char** audio_fm_list=NULL; // override audio codec family
135 static char** video_fm_list=NULL; // override video codec family
136 extern char *demuxer_name; // override demuxer
137 extern char *audio_demuxer_name; // override audio demuxer
138 extern char *sub_demuxer_name; // override sub demuxer
140 static int out_audio_codec=-1;
141 static int out_video_codec=-1;
143 int out_file_format=MUXER_TYPE_AVI; // default to AVI
144 int quiet=0;
145 double video_time_usage=0;
146 double vout_time_usage=0;
147 double max_video_time_usage=0;
148 double max_vout_time_usage=0;
149 double cur_video_time_usage=0;
150 double cur_vout_time_usage=0;
151 int benchmark=0;
153 #include "osdep/priority.h"
155 // A-V sync:
156 int delay_corrected=1;
157 static float default_max_pts_correction=-1;//0.01f;
158 static float max_pts_correction=0;//default_max_pts_correction;
159 static float c_total=0;
161 static float audio_preload=0.5;
162 static float audio_delay_fix=0.0;
163 static float audio_delay=0.0;
164 static int ignore_start=0;
165 static int audio_density=2;
167 double force_fps=0;
168 static double force_ofps=0; // set to 24 for inverse telecine
169 static int skip_limit=-1;
171 static int force_srate=0;
172 static int audio_output_format=0;
174 char *vobsub_out=NULL;
175 unsigned int vobsub_out_index=0;
176 char *vobsub_out_id=NULL;
178 char* out_filename=NULL;
180 char *force_fourcc=NULL;
181 int force_audiofmttag=-1;
183 char* passtmpfile="divx2pass.log";
185 static int play_n_frames=-1;
186 static int play_n_frames_mf=-1;
188 #include "libvo/font_load.h"
189 #include "libvo/sub.h"
191 // sub:
192 char *font_name=NULL;
193 char *sub_font_name=NULL;
194 extern int font_fontconfig;
195 float font_factor=0.75;
196 char **sub_name=NULL;
197 float sub_delay=0;
198 float sub_fps=0;
199 int sub_auto = 0;
200 int subcc_enabled=0;
201 int suboverlap_enabled = 1;
203 int auto_expand=1;
204 int encode_duplicates=1;
206 // infos are empty by default
207 char *info_name=NULL;
208 char *info_artist=NULL;
209 char *info_genre=NULL;
210 char *info_subject=NULL;
211 char *info_copyright=NULL;
212 char *info_sourceform=NULL;
213 char *info_comment=NULL;
215 // Needed by libmpcodecs vf_vo.c
216 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
217 uint32_t d_width, uint32_t d_height, uint32_t flags,
218 char *title, uint32_t format) { abort(); }
219 int vo_control(struct vo *vo, uint32_t request, void *data) { abort(); }
220 void vo_seek_reset(struct vo *vo) { abort(); }
221 int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts) { abort(); }
222 int vo_draw_frame(struct vo *vo, uint8_t *src[]) { abort(); }
223 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y) { abort(); }
224 void vo_draw_osd(struct vo *vo, struct osd_state *osd) { abort(); }
225 void vo_flip_page(struct vo *vo, uint32_t pts_us, int duration) { abort(); }
226 void vo_check_events(struct vo *vo) { abort(); }
228 // Needed by getch2
229 struct mp_fifo;
230 void mplayer_put_key(struct mp_fifo *fifo, int code)
234 #include "ass_mp.h"
235 char *current_module;
236 #include "mpcommon.h"
238 // Needed by mpcommon.c
239 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs) {
240 vo_sub = subs;
241 vo_osd_changed(OSDTYPE_SUBTITLE);
244 //-------------------------- config stuff:
246 m_config_t* mconfig;
248 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
250 static int cfg_include(m_option_t *conf, char *filename){
251 return m_config_parse_config_file(mconfig, filename);
254 static double seek_to_sec;
255 static off_t seek_to_byte=0;
257 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
259 static char * frameno_filename=NULL;
261 typedef struct {
262 unsigned char* start;
263 int in_size;
264 float frame_time;
265 int already_read;
266 } s_frame_data;
268 #include "edl.h"
269 static edl_record_ptr edl_records = NULL; ///< EDL entries memory area
270 static edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
271 static short edl_muted; ///< Stores whether EDL is currently in muted mode.
272 static short edl_seeking; ///< When non-zero, stream is seekable.
273 static short edl_seek_type; ///< When non-zero, frames are discarded instead of seeking.
275 #include "cfg-mencoder.h"
277 #include "spudec.h"
278 #include "vobsub.h"
280 #include "libao2/audio_out.h"
281 /* FIXME */
282 static void mencoder_exit(int level, const char *how)
284 if (how)
285 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nExiting... (%s)\n", how);
286 else
287 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nExiting...\n");
289 exit(level);
292 static void parse_cfgfiles( m_config_t* conf )
294 char *conffile;
295 if (!disable_system_conf &&
296 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mencoder.conf") < 0)
297 mencoder_exit(1,_("config file error"));
299 if (!disable_user_conf) {
300 if ((conffile = get_path("mencoder.conf")) == NULL) {
301 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
302 } else {
303 if (m_config_parse_config_file(conf, conffile) < 0)
304 mencoder_exit(1,_("config file error"));
305 free(conffile);
311 //---------------------------------------------------------------------------
313 static int dec_audio(sh_audio_t *sh_audio,unsigned char* buffer,int total){
314 int size=0;
315 int at_eof=0;
316 while(size<total && !at_eof){
317 int len=total-size;
318 if (decode_audio(sh_audio, len) < 0) at_eof=1;
319 if(len>sh_audio->a_out_buffer_len) len=sh_audio->a_out_buffer_len;
320 fast_memcpy(buffer+size,sh_audio->a_out_buffer,len);
321 sh_audio->a_out_buffer_len-=len; size+=len;
322 if(sh_audio->a_out_buffer_len>0)
323 fast_memcpy(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[len],sh_audio->a_out_buffer_len);
325 return size;
328 //---------------------------------------------------------------------------
330 static volatile int at_eof=0;
331 static volatile int interrupted=0;
333 static void exit_sighandler(int x){
334 at_eof=1;
335 interrupted=2; /* 1 means error */
338 static muxer_t* muxer=NULL;
340 static void add_subtitles(char *filename, float fps, int silent)
342 sub_data *subd;
343 #ifdef CONFIG_ASS
344 ASS_Track *asst = 0;
345 #endif
347 if (!filename) return;
349 subd = sub_read_file(filename, fps);
350 #ifdef CONFIG_ASS
351 if (opts.ass_enabled)
352 #ifdef CONFIG_ICONV
353 asst = ass_read_file(ass_library, filename, sub_cp);
354 #else
355 asst = ass_read_file(ass_library, filename, 0);
356 #endif
357 if (opts.ass_enabled && subd && !asst)
358 asst = ass_read_subdata(ass_library, subd, fps);
360 if (!asst && !subd && !silent)
361 #else
362 if (!subd && !silent)
363 #endif
364 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot load subtitles: %s\n",
365 filename_recode(filename));
367 #ifdef CONFIG_ASS
368 if (!asst && !subd) return;
369 ass_track = asst;
370 #else
371 if (!subd) return;
372 #endif
373 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
374 filename_recode(filename));
375 subdata = subd;
378 /* This function returns the absolute time for which MEncoder will switch files
379 * or move in the file so audio can be cut correctly. -1 if there is no limit. */
380 static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v)
382 float timeleft = -1;
383 if (play_n_frames >= 0) timeleft = mux_v->timer + play_n_frames * (double)(mux_v->h.dwScale) / mux_v->h.dwRate;
384 if (end_at.type == END_AT_TIME && (timeleft > end_at.pos || timeleft == -1)) timeleft = end_at.pos;
385 if (next_edl_record && demuxer && demuxer->video) { // everything is OK to be checked
386 float tmp = mux_v->timer + next_edl_record->start_sec - demuxer->video->pts;
387 if (timeleft == -1 || timeleft > tmp) {
388 // There's less time in EDL than what we already know
389 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
390 timeleft = tmp;
391 } else if (next_edl_record->action == EDL_MUTE) {
392 //timeleft = next_edl_record->start_sec - demuxer->video->pts;
393 // For the moment (and probably forever) EDL mute doesn't work in MEncoder
397 return timeleft;
400 /// Returns a_pts
401 static float calc_a_pts(demux_stream_t *d_audio)
403 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
404 float a_pts = 0.;
405 if (sh_audio)
406 a_pts = d_audio->pts + (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
407 return a_pts;
410 /** \brief Seeks audio forward to pts by dumping audio packets
411 * \return The current audio pts. */
412 static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a)
414 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
415 int samplesize, avg;
416 float a_pts = calc_a_pts(d_audio);
418 if (!sh_audio) return a_pts;
420 if (sh_audio->audio.dwScale) samplesize = sh_audio->audio.dwSampleSize;
421 else samplesize = (sh_audio->wf ? sh_audio->wf->nBlockAlign : 1);
422 avg = (sh_audio->wf ? sh_audio->wf->nAvgBytesPerSec : sh_audio->i_bps);
424 // after a demux_seek, a_pts will be zero until you read some audio.
425 // carefully checking if a_pts is truely correct by reading tiniest amount of data possible.
426 if (pts > a_pts && a_pts == 0.0 && samplesize) {
427 if (demux_read_data(sh_audio->ds,mux_a->buffer,samplesize) <= 0) return a_pts; // EOF
428 a_pts = calc_a_pts(d_audio);
431 while (pts > a_pts) {
432 int len;
433 if (samplesize) {
434 len = avg * (pts - a_pts > 0.5 ? 0.5 : pts - a_pts);
435 len/= samplesize; if(len<1) len=1;
436 len*= samplesize;
437 len = demux_read_data(sh_audio->ds,mux_a->buffer,len);
438 } else {
439 unsigned char * crap;
440 len = ds_get_packet(sh_audio->ds, &crap);
442 if (len <= 0) break; // EOF of audio.
443 a_pts = calc_a_pts(d_audio);
445 return a_pts;
448 /** \brief Seeks slowly by dumping frames.
449 * \return 1 for success, 2 for EOF. */
450 static int slowseek(float end_pts, demux_stream_t *d_video,
451 demux_stream_t *d_audio, muxer_stream_t *mux_a,
452 s_frame_data *frame_data, int framecopy, int print_info)
454 sh_video_t * sh_video = d_video->sh;
455 vf_instance_t * vfilter = sh_video ? sh_video->vfilter : NULL;
456 int done = 0;
458 while (!interrupted) {
459 float a_pts = 0.;
461 if (!frame_data->already_read) { // when called after fixdelay, a frame is already read
462 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
463 if(frame_data->in_size<0) return 2;
464 sh_video->timer += frame_data->frame_time;
466 frame_data->already_read = 0;
468 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
470 if (done) {
471 // wait for keyframe in case of -ovc copy
472 if (!framecopy || (sh_video->ds->flags & 1)) {
473 frame_data->already_read = 1;
474 return 1;
477 if (sh_video->pts >= end_pts) done = 1;
479 if (vfilter) {
480 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
481 osd);
482 int softskip = (vfilter->control(vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) == CONTROL_TRUE);
483 void *decoded_frame = decode_video(sh_video, frame_data->start, frame_data->in_size, !softskip, MP_NOPTS_VALUE);
484 if (decoded_frame)
485 if (filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE)) {
486 struct vf_instance *vf = sh_video->vfilter;
487 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
488 vf->control(vf, VFCTRL_DRAW_OSD, osd);
492 if (print_info) mp_tmsg(MSGT_MENCODER, MSGL_STATUS,
493 "EDL SKIP: Start: %.2f End: %.2f Current: V: %.2f A: %.2f \r",
494 next_edl_record->start_sec, next_edl_record->stop_sec,
495 sh_video->pts, a_pts);
497 if (interrupted) return 2;
498 return 1;
501 /// Deletes audio or video as told by -delay to sync
502 static void fixdelay(demux_stream_t *d_video, demux_stream_t *d_audio,
503 muxer_stream_t *mux_a, s_frame_data *frame_data,
504 int framecopy)
506 // TODO: Find a way to encode silence instead of deleting video
507 sh_video_t * sh_video = d_video->sh;
508 float a_pts;
510 // demux_seek has a weirdness that sh_video->pts is meaningless,
511 // until a single frame is read... Same for audio actually too.
512 // Reading one frame, and keeping it.
513 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
514 if(frame_data->in_size<0) return;
515 sh_video->timer += frame_data->frame_time;
516 frame_data->already_read = 1;
518 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
520 if (audio_delay > 0) return;
521 else if (sh_video->pts - frame_data->frame_time + audio_delay >= a_pts) return;
523 slowseek(a_pts - audio_delay, d_video, d_audio, mux_a, frame_data, framecopy, 0);
526 /** \brief Seeks for EDL
527 * \return 1 for success, 0 for failure, 2 for EOF. */
528 static int edl_seek(edl_record_ptr next_edl_record, demuxer_t *demuxer,
529 demux_stream_t *d_audio, muxer_stream_t *mux_a,
530 s_frame_data *frame_data, int framecopy)
532 sh_video_t * sh_video = demuxer->video ? demuxer->video->sh : NULL;
534 if (!sh_video) return 0;
535 if (sh_video->pts >= next_edl_record->stop_sec) return 1; // nothing to do...
537 if (!edl_seek_type) {
538 if(demux_seek(demuxer, next_edl_record->stop_sec - sh_video->pts, audio_delay, 0)){
539 sh_video->pts = demuxer->video->pts;
540 //if (vo_vobsub) vobsub_seek(vo_vobsub,sh_video->pts);
541 resync_video_stream(sh_video);
542 //if(vo_spudec) spudec_reset(vo_spudec);
543 if (audio_delay != 0.0) fixdelay(demuxer->video, d_audio, mux_a, frame_data, framecopy);
544 return 1;
546 // non-seekable stream.
547 return 0;
550 // slow seek, read every frame.
552 return slowseek(next_edl_record->stop_sec, demuxer->video, d_audio, mux_a, frame_data, framecopy, 1);
556 int main(int argc,char* argv[]){
558 stream_t* stream=NULL;
559 stream_t* ostream=NULL;
560 demuxer_t* demuxer=NULL;
561 stream_t* stream2=NULL;
562 demuxer_t* demuxer2=NULL;
563 demux_stream_t *d_audio=NULL;
564 demux_stream_t *d_video=NULL;
565 demux_stream_t *d_dvdsub=NULL;
566 sh_audio_t *sh_audio=NULL;
567 sh_video_t *sh_video=NULL;
568 int file_format=DEMUXER_TYPE_UNKNOWN;
569 int i=DEMUXER_TYPE_UNKNOWN;
570 void *vobsub_writer=NULL;
571 s_frame_data frame_data = { .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
573 uint32_t ptimer_start;
574 uint32_t audiorate=0;
575 uint32_t videorate=0;
576 uint32_t audiosamples=1;
577 uint32_t videosamples=1;
578 uint32_t skippedframes=0;
579 uint32_t duplicatedframes=0;
580 uint32_t badframes=0;
582 muxer_stream_t* mux_a=NULL;
583 muxer_stream_t* mux_v=NULL;
584 off_t muxer_f_size=0;
586 double v_pts_corr=0;
587 double v_timer_corr=0;
589 m_entry_t* filelist = NULL;
590 char* filename=NULL;
592 int decoded_frameno=0;
593 int next_frameno=-1;
594 int curfile=0;
595 int new_srate=0;
597 unsigned int timer_start=0;
598 ao_data_t ao_data = {0,0,0,0,OUTBURST,-1,0};
600 audio_encoding_params_t aparams;
601 audio_encoder_t *aencoder = NULL;
603 mp_msg_init();
604 set_av_log_callback();
606 // Create the config context and register the options
607 set_default_mencoder_options(&opts);
608 mconfig = m_config_new(&opts, cfg_include);
609 m_config_register_options(mconfig,mencoder_opts);
611 // Preparse the command line
612 m_config_preparse_command_line(mconfig,argc,argv);
614 print_version("MEncoder");
616 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
617 set_path_env();
618 #endif
620 InitTimer();
622 // check codec.conf
623 if(!codecs_file || !parse_codec_cfg(codecs_file)){
624 if(!parse_codec_cfg(get_path("codecs.conf"))){
625 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
626 if(!parse_codec_cfg(NULL)){
627 mencoder_exit(1,NULL);
629 mp_tmsg(MSGT_MENCODER,MSGL_V,"Using built-in default codecs.conf.\n");
634 parse_cfgfiles(mconfig);
635 filelist = m_config_parse_me_command_line(mconfig, argc, argv);
636 if(!filelist) mencoder_exit(1, _("error parsing command line"));
639 char *extension;
641 if (!out_filename) mencoder_exit(1,_("No output file specified, please see the -o option."));
642 extension=strrchr(out_filename,'.');
643 if (extension != NULL && strlen(extension) > 3 && strlen(extension) < 6)
645 extension++;
647 switch (out_file_format)
649 case MUXER_TYPE_AVI:
650 if (strcasecmp(extension,"avi"))
651 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\nWARNING: OUTPUT FILE FORMAT IS _AVI_. See -of help.\n");
652 break;
654 case MUXER_TYPE_MPEG:
655 if (strcasecmp(extension,"mpg") &&
656 strcasecmp(extension,"mpeg") &&
657 strcasecmp(extension,"vob"))
658 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\nWARNING: OUTPUT FILE FORMAT IS _MPEG_. See -of help.\n");
659 break;
663 /* Display what configure line was used */
664 mp_msg(MSGT_MENCODER, MSGL_V, "Configuration: " CONFIGURATION "\n");
666 #define FormatNotRecognized _("============ Sorry, this file format is not recognized/supported =============\n"\
667 "=== If this file is an AVI, ASF or MPEG stream, please contact the author! ===\n")
670 if (frameno_filename) {
671 stream2=open_stream(frameno_filename,0,&i);
672 if(stream2){
673 demuxer2=demux_open(&opts, stream2,DEMUXER_TYPE_AVI,-1,-1,-2,NULL);
674 if(demuxer2) mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Using pass3 control file: %s\n", frameno_filename);
675 else mp_tmsg(MSGT_DEMUXER,MSGL_ERR, FormatNotRecognized);
679 #ifdef CONFIG_PRIORITY
680 set_priority();
681 #endif
683 if (codec_path)
684 set_codec_path(codec_path);
686 // check font
687 #ifdef CONFIG_FREETYPE
688 init_freetype();
689 #endif
690 #ifdef CONFIG_FONTCONFIG
691 if(font_fontconfig <= 0)
693 #endif
694 #ifdef CONFIG_BITMAP_FONT
695 if(font_name){
696 vo_font=read_font_desc(font_name,font_factor,verbose>1);
697 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",font_name);
698 } else {
699 // try default:
700 vo_font=read_font_desc(get_path("font/font.desc"),font_factor,verbose>1);
701 if(!vo_font)
702 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
704 #endif
705 #ifdef CONFIG_FONTCONFIG
707 #endif
709 osd = osd_create();
711 /* HACK, for some weird reason, push() has to be called twice,
712 otherwise options are not saved correctly */
713 m_config_push(mconfig);
714 play_next_file:
715 m_config_push(mconfig);
716 m_entry_set_options(mconfig,&filelist[curfile]);
717 filename = filelist[curfile].name;
719 #ifdef CONFIG_ASS
720 ass_library = ass_init();
721 #endif
723 if(!filename){
724 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "\nFilename missing.\n\n");
725 mencoder_exit(1,NULL);
727 stream=open_stream(filename,0,&file_format);
729 if(!stream){
730 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Cannot open file/device.\n");
731 mencoder_exit(1,NULL);
734 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "success: format: %d data: 0x%X - 0x%x\n", file_format, (int)(stream->start_pos), (int)(stream->end_pos));
736 #ifdef CONFIG_DVDREAD
737 if(stream->type==STREAMTYPE_DVD){
738 if(audio_lang && opts.audio_id==-1) opts.audio_id=dvd_aid_from_lang(stream,audio_lang);
739 if(dvdsub_lang && opts.sub_id==-1) opts.sub_id=dvd_sid_from_lang(stream,dvdsub_lang);
741 #endif
743 #ifdef CONFIG_DVDNAV
744 if(stream->type==STREAMTYPE_DVDNAV){
745 if(audio_lang && opts.audio_id==-1) opts.audio_id=mp_dvdnav_aid_from_lang(stream,audio_lang);
746 if(dvdsub_lang && opts.sub_id==-1) opts.sub_id=mp_dvdnav_sid_from_lang(stream,dvdsub_lang);
748 #endif
750 stream->start_pos+=seek_to_byte;
752 if(stream_cache_size>0) stream_enable_cache(stream,stream_cache_size*1024,0,0);
754 if(demuxer2) opts.audio_id=-2; /* do NOT read audio packets... */
756 demuxer=demux_open(&opts, stream,file_format,opts.audio_id,opts.video_id,opts.sub_id,filename);
757 if(!demuxer){
758 mp_tmsg(MSGT_DEMUXER, MSGL_FATAL, FormatNotRecognized);
759 mp_tmsg(MSGT_DEMUXER, MSGL_FATAL, "Cannot open demuxer.\n");
760 mencoder_exit(1,NULL);
763 if (ts_prog) {
764 demux_program_t prog = { .progid = ts_prog };
765 if (demux_control(demuxer, DEMUXER_CTRL_IDENTIFY_PROGRAM, &prog) != DEMUXER_CTRL_NOTIMPL) {
766 opts.audio_id = prog.aid; // switching is handled by select_audio below
767 opts.video_id = prog.vid;
768 demuxer_switch_video(demuxer, opts.video_id);
771 select_audio(demuxer, opts.audio_id, audio_lang);
773 if (opts.sub_id < -1 && dvdsub_lang)
774 opts.sub_id = demuxer_sub_track_by_lang(demuxer, dvdsub_lang);
776 if (opts.sub_id < -1)
777 opts.sub_id = demuxer_default_sub_track(demuxer);
779 for (i = 0; i < MAX_S_STREAMS; i++) {
780 sh_sub_t *sh = demuxer->s_streams[i];
781 if (sh && sh->sid == opts.sub_id) {
782 demuxer->sub->id = i;
783 demuxer->sub->sh = sh;
784 break;
788 if(opts.chapterrange[0]>1) {
789 double pts;
790 if (demuxer_seek_chapter(demuxer, opts.chapterrange[0]-1, &pts, NULL) >= 0 && pts > -1.0)
791 seek_to_sec = pts;
794 d_audio=demuxer2 ? demuxer2->audio : demuxer->audio;
795 d_video=demuxer->video;
796 d_dvdsub=demuxer->sub;
797 sh_audio=d_audio->sh;
798 sh_video=d_video->sh;
800 if(!sh_video)
802 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Video stream is mandatory!\n");
803 mencoder_exit(1,NULL);
806 if(!video_read_properties(sh_video)){
807 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Video: Cannot read properties.\n");
808 mencoder_exit(1,NULL);
811 mp_tmsg(MSGT_MENCODER,MSGL_INFO, "[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
812 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
813 sh_video->fps,sh_video->frametime
816 if(force_fps){
817 sh_video->fps=force_fps;
818 sh_video->frametime=1.0f/sh_video->fps;
819 mp_tmsg(MSGT_MENCODER,MSGL_INFO,"Input fps will be interpreted as %5.3f instead.\n", sh_video->fps);
822 if(sh_audio && out_audio_codec<0){
823 if(opts.audio_id==-2)
824 mp_tmsg(MSGT_MENCODER,MSGL_ERR,"This demuxer doesn't support -nosound yet.\n");
825 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nNo audio encoder (-oac) selected. Select one (see -oac help) or use -nosound.\n");
826 mencoder_exit(1,NULL);
828 if(sh_video && out_video_codec<0){
829 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nNo video encoder (-ovc) selected. Select one (see -ovc help).\n");
830 mencoder_exit(1,NULL);
833 if(sh_audio && (out_audio_codec || seek_to_sec || !sh_audio->wf || opts.playback_speed != 1.0)){
834 // Go through the codec.conf and find the best codec...
835 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
836 if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
837 sh_audio=d_audio->sh=NULL; // failed to init :(
839 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
842 if (sh_audio) {
843 new_srate = sh_audio->samplerate;
844 if (opts.playback_speed != 1.0) {
845 new_srate *= opts.playback_speed;
846 // limits are taken from libaf/af_resample.c
847 if (new_srate < 8000) new_srate = 8000;
848 if (new_srate > 192000) new_srate = 192000;
849 opts.playback_speed = (float)new_srate / (float)sh_audio->samplerate;
853 // set up video encoder:
855 if (!curfile) { // curfile is non zero when a second file is opened
856 if (vobsub_out) {
857 unsigned int palette[16], width, height;
858 unsigned char tmp[3] = { 0, 0, 0 };
859 if (spudec_ifo && vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, opts.sub_id, tmp) >= 0)
860 vobsub_writer = vobsub_out_open(vobsub_out, palette, sh_video->disp_w, sh_video->disp_h,
861 vobsub_out_id?vobsub_out_id:(char *)tmp, vobsub_out_index);
862 #ifdef CONFIG_DVDREAD
863 if (vobsub_writer == NULL) {
864 char tmp[3];
865 if (vobsub_out_id == NULL && stream->type == STREAMTYPE_DVD) {
866 int i;
867 dvd_priv_t *dvd = (dvd_priv_t*)stream->priv;
868 for (i = 0; i < dvd->nr_of_subtitles; ++i)
869 if (dvd->subtitles[i].id == opts.sub_id) {
870 tmp[0] = (dvd->subtitles[i].language >> 8) & 0xff;
871 tmp[1] = dvd->subtitles[i].language & 0xff;
872 tmp[2] = 0;
873 vobsub_out_id = tmp;
874 break;
877 vobsub_writer=vobsub_out_open(vobsub_out, stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
878 sh_video->disp_w, sh_video->disp_h, vobsub_out_id, vobsub_out_index);
880 #endif
882 else {
883 if (spudec_ifo) {
884 unsigned int palette[16], width, height;
885 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
886 vo_spudec=spudec_new_scaled(palette, sh_video->disp_w, sh_video->disp_h, NULL, 0);
888 #ifdef CONFIG_DVDREAD
889 if (vo_spudec==NULL) {
890 vo_spudec=spudec_new_scaled(stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
891 sh_video->disp_w, sh_video->disp_h, NULL, 0);
893 #endif
894 if (vo_spudec)
895 spudec_set_forced_subs_only(vo_spudec, forced_subs_only);
898 ostream = open_output_stream(out_filename, 0);
899 if(!ostream) {
900 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, "Cannot open output file '%s'.\n", out_filename);
901 mencoder_exit(1,NULL);
904 muxer=muxer_new_muxer(out_file_format,ostream);
905 if(!muxer) {
906 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, "Cannot initialize muxer.");
907 mencoder_exit(1,NULL);
909 #if 0
910 //disabled: it horrybly distorts filtered sound
911 if(out_file_format == MUXER_TYPE_MPEG) audio_preload = 0;
912 #endif
914 muxer->audio_delay_fix = audio_delay_fix;
916 // ============= VIDEO ===============
918 mux_v=muxer_new_stream(muxer,MUXER_TYPE_VIDEO);
920 mux_v->buffer_size=0x200000; // 2MB
921 mux_v->buffer=malloc(mux_v->buffer_size);
923 mux_v->source=sh_video;
925 mux_v->h.dwSampleSize=0; // VBR
926 #ifdef CONFIG_LIBAVCODEC
928 double fps = force_ofps?force_ofps:sh_video->fps*opts.playback_speed;
929 AVRational q= av_d2q(fps, fps*1001+2);
930 mux_v->h.dwScale= q.den;
931 mux_v->h.dwRate = q.num;
933 #else
934 mux_v->h.dwScale=10000;
935 mux_v->h.dwRate=mux_v->h.dwScale*(force_ofps?force_ofps:sh_video->fps*opts.playback_speed);
936 #endif
938 mux_v->codec=out_video_codec;
940 mux_v->bih=NULL;
942 sh_video->codec=NULL;
943 sh_video->vfilter=NULL; // fixme!
945 switch(mux_v->codec){
946 case VCODEC_COPY:
947 if (!curfile) {
948 if (sh_video->bih) {
949 mux_v->bih=malloc(sh_video->bih->biSize);
950 memcpy(mux_v->bih, sh_video->bih, sh_video->bih->biSize);
952 else
954 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
955 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
956 mux_v->bih->biWidth=sh_video->disp_w;
957 mux_v->bih->biHeight=sh_video->disp_h;
958 mux_v->bih->biCompression=sh_video->format;
959 mux_v->bih->biPlanes=1;
960 mux_v->bih->biBitCount=24; // FIXME!!!
961 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
964 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
965 mux_v->bih->biWidth, mux_v->bih->biHeight,
966 mux_v->bih->biBitCount, mux_v->bih->biCompression);
968 if (curfile) {
969 if (sh_video->bih) {
970 if ((mux_v->bih->biSize != sh_video->bih->biSize) ||
971 memcmp(mux_v->bih, sh_video->bih, sh_video->bih->biSize))
973 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
974 sh_video->bih->biWidth, sh_video->bih->biHeight,
975 sh_video->bih->biBitCount, sh_video->bih->biCompression);
976 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll video files must have identical fps, resolution, and codec for -ovc copy.\n");
977 mencoder_exit(1,NULL);
980 else {
981 if ((mux_v->bih->biWidth != sh_video->disp_w) ||
982 (mux_v->bih->biHeight != sh_video->disp_h) ||
983 (mux_v->bih->biCompression != sh_video->format)) {
984 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
985 sh_video->disp_w, sh_video->disp_w, 24, sh_video->format);
986 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll video files must have identical fps, resolution, and codec for -ovc copy.\n");
987 mencoder_exit(1,NULL);
991 break;
992 case VCODEC_FRAMENO:
993 if (!curfile) {
994 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
995 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
996 mux_v->bih->biWidth=sh_video->disp_w;
997 mux_v->bih->biHeight=sh_video->disp_h;
998 mux_v->bih->biPlanes=1;
999 mux_v->bih->biBitCount=24;
1000 mux_v->bih->biCompression=mmioFOURCC('F','r','N','o');
1001 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
1003 break;
1004 default: {
1005 static vf_instance_t * ve = NULL;
1006 if (!ve) {
1007 switch(mux_v->codec){
1008 case VCODEC_LIBAVCODEC:
1009 sh_video->vfilter=vf_open_encoder(&opts, NULL,"lavc",(char *)mux_v); break;
1010 case VCODEC_RAW:
1011 sh_video->vfilter=vf_open_encoder(&opts, NULL,"raw",(char *)mux_v); break;
1012 case VCODEC_VFW:
1013 sh_video->vfilter=vf_open_encoder(&opts, NULL,"vfw",(char *)mux_v); break;
1014 case VCODEC_LIBDV:
1015 sh_video->vfilter=vf_open_encoder(&opts, NULL,"libdv",(char *)mux_v); break;
1016 case VCODEC_XVID:
1017 sh_video->vfilter=vf_open_encoder(&opts, NULL,"xvid",(char *)mux_v); break;
1018 case VCODEC_QTVIDEO:
1019 sh_video->vfilter=vf_open_encoder(&opts, NULL,"qtvideo",(char *)mux_v); break;
1020 case VCODEC_NUV:
1021 sh_video->vfilter=vf_open_encoder(&opts, NULL,"nuv",(char *)mux_v); break;
1022 case VCODEC_X264:
1023 sh_video->vfilter=vf_open_encoder(&opts, NULL,"x264",(char *)mux_v); break;
1025 if(!mux_v->bih || !sh_video->vfilter){
1026 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"Failed to open the encoder.\n");
1027 mencoder_exit(1,NULL);
1029 ve = sh_video->vfilter;
1030 } else sh_video->vfilter = ve;
1031 // append 'expand' filter, it fixes stride problems and renders osd:
1032 #ifdef CONFIG_ASS
1033 if (auto_expand && !opts.ass_enabled) { /* we do not want both */
1034 #else
1035 if (auto_expand) {
1036 #endif
1037 char* vf_args[] = { "osd", "1", NULL };
1038 sh_video->vfilter=vf_open_filter(&opts, sh_video->vfilter,"expand",vf_args);
1041 #ifdef CONFIG_ASS
1042 if(opts.ass_enabled) {
1043 int i;
1044 int insert = 1;
1045 if (opts.vf_settings)
1046 for (i = 0; opts.vf_settings[i].name; ++i)
1047 if (strcmp(opts.vf_settings[i].name, "ass") == 0) {
1048 insert = 0;
1049 break;
1051 if (insert) {
1052 extern vf_info_t vf_info_ass;
1053 vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
1054 char* vf_arg[] = {"auto", "1", NULL};
1055 vf_instance_t* vf_ass = vf_open_plugin(&opts,libass_vfs,sh_video->vfilter,"ass",vf_arg);
1056 if (vf_ass)
1057 sh_video->vfilter=(void*)vf_ass;
1058 else
1059 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
1062 if (ass_library) {
1063 for (i = 0; i < demuxer->num_attachments; ++i) {
1064 demux_attachment_t* att = demuxer->attachments + i;
1065 if (use_embedded_fonts &&
1066 att->name && att->type && att->data && att->data_size &&
1067 (strcmp(att->type, "application/x-truetype-font") == 0 ||
1068 strcmp(att->type, "application/x-font") == 0))
1069 ass_add_font(ass_library, att->name, att->data, att->data_size);
1073 #endif
1075 sh_video->vfilter=append_filters(sh_video->vfilter, opts.vf_settings);
1077 #ifdef CONFIG_ASS
1078 if (opts.ass_enabled)
1079 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
1080 #endif
1082 // after reading video params we should load subtitles because
1083 // we know fps so now we can adjust subtitles time to ~6 seconds AST
1084 // check .sub
1085 if(sub_name && sub_name[0]){
1086 for (i = 0; sub_name[i] != NULL; ++i)
1087 add_subtitles (sub_name[i], sh_video->fps, 0);
1088 } else
1089 if(sub_auto && filename) { // auto load sub file ...
1090 char **tmp = NULL;
1091 int i = 0;
1092 char *psub = get_path( "sub/" );
1093 tmp = sub_filenames((psub ? psub : ""), filename);
1094 free(psub);
1095 while (tmp[i])
1097 add_subtitles (tmp[i], sh_video->fps, 0);
1098 free(tmp[i++]);
1100 free(tmp);
1103 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1104 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
1105 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1106 if(!sh_video->initialized) mencoder_exit(1,NULL);
1110 if (!curfile) {
1111 /* force output fourcc to .. */
1112 if ((force_fourcc != NULL) && (strlen(force_fourcc) >= 4))
1114 mux_v->bih->biCompression = mmioFOURCC(force_fourcc[0], force_fourcc[1],
1115 force_fourcc[2], force_fourcc[3]);
1116 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Forcing output FourCC to %x [%.4s].\n",
1117 mux_v->bih->biCompression, (char *)&mux_v->bih->biCompression);
1120 if (! ignore_start)
1121 muxer->audio_delay_fix -= sh_video->stream_delay;
1123 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
1125 // ============= AUDIO ===============
1126 if(sh_audio){
1128 if (force_audiofmttag != -1) {
1129 sh_audio->format = force_audiofmttag;
1130 if (sh_audio->wf) {
1131 sh_audio->wf->wFormatTag = sh_audio->format;
1133 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Forcing output audio format tag to 0x%x.\n",
1134 force_audiofmttag);
1137 mux_a=muxer_new_stream(muxer,MUXER_TYPE_AUDIO);
1139 mux_a->buffer_size=0x100000; //16384;
1140 mux_a->buffer=malloc(mux_a->buffer_size);
1141 if (!mux_a->buffer)
1142 mencoder_exit(1,_("Memory allocation failed.\n"));
1144 mux_a->source=sh_audio;
1146 mux_a->codec=out_audio_codec;
1148 ao_data.samplerate = force_srate;
1149 ao_data.channels = 0;
1150 ao_data.format = audio_output_format;
1151 if(!init_audio_filters(sh_audio,
1152 // input:
1153 new_srate,
1154 // output:
1155 &ao_data.samplerate, &ao_data.channels, &ao_data.format)) {
1156 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Error at audio filter chain pre-init!\n");
1157 mencoder_exit(1, NULL);
1160 aparams.channels = ao_data.channels;
1161 aparams.sample_rate = ao_data.samplerate;
1162 aparams.audio_preload = 1000 * audio_preload;
1163 if(mux_a->codec != ACODEC_COPY) {
1164 aencoder = new_audio_encoder(mux_a, &aparams);
1165 if(!aencoder)
1166 mencoder_exit(1, NULL);
1167 if(!init_audio_filters(sh_audio,
1168 new_srate,
1169 &aparams.sample_rate, &aparams.channels, &aencoder->input_format)) {
1170 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Couldn't find matching filter/ao format!\n");
1171 mencoder_exit(1,NULL);
1175 #define NoSpeedWithFrameCopy _("WARNING: -speed is not guaranteed to work correctly with -oac copy!\n"\
1176 "Your encode might be broken!\n")
1178 switch(mux_a->codec){
1179 case ACODEC_COPY:
1180 if (opts.playback_speed != 1.0) mp_tmsg(MSGT_CPLAYER, MSGL_WARN, NoSpeedWithFrameCopy);
1181 if (sh_audio->format >= 0x10000) {
1182 mp_tmsg(MSGT_MENCODER,MSGL_ERR,"Audio format 0x%x is incompatible with '-oac copy', please try '-oac pcm' instead or use '-fafmttag' to override it.\n", sh_audio->format);
1183 mencoder_exit(1,NULL);
1185 if (sh_audio->wf){
1186 mux_a->wf=malloc(sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
1187 memcpy(mux_a->wf, sh_audio->wf, sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
1188 if(!sh_audio->i_bps) sh_audio->i_bps=mux_a->wf->nAvgBytesPerSec;
1189 } else {
1190 mux_a->wf = malloc(sizeof(WAVEFORMATEX));
1191 mux_a->wf->nBlockAlign = 1; //mux_a->h.dwSampleSize;
1192 mux_a->wf->wFormatTag = sh_audio->format;
1193 mux_a->wf->nChannels = sh_audio->channels;
1194 mux_a->wf->nSamplesPerSec = sh_audio->samplerate;
1195 mux_a->wf->nAvgBytesPerSec=sh_audio->i_bps; //mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
1196 mux_a->wf->wBitsPerSample = 16; // FIXME
1197 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
1199 if(sh_audio->audio.dwScale){
1200 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize;
1201 mux_a->h.dwScale=sh_audio->audio.dwScale;
1202 mux_a->h.dwRate=sh_audio->audio.dwRate;
1203 } else {
1204 mux_a->h.dwSampleSize=mux_a->wf->nBlockAlign;
1205 mux_a->h.dwScale=mux_a->h.dwSampleSize;
1206 mux_a->h.dwRate=mux_a->wf->nAvgBytesPerSec;
1208 mux_a->h.dwRate *= opts.playback_speed;
1209 mux_a->wf->nSamplesPerSec *= opts.playback_speed;
1210 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1211 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
1212 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
1213 break;
1216 if ( mp_msg_test(MSGT_MENCODER,MSGL_DBG2) ) print_wave_header(mux_a->wf, MSGL_DBG2);
1218 if (! ignore_start)
1219 muxer->audio_delay_fix += sh_audio->stream_delay;
1221 } // if(sh_audio)
1223 decoded_frameno=0;
1225 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
1226 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
1227 signal(SIGTERM,exit_sighandler); // kill
1228 signal(SIGHUP,exit_sighandler); // broken terminal line
1229 signal(SIGPIPE,exit_sighandler); // broken pipe
1231 timer_start=GetTimerMS();
1232 } // if (!curfile) // if this was the first file.
1233 else {
1234 if (!mux_a != !sh_audio) {
1235 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nCannot mix video-only files with audio and video files. Try -nosound.\n");
1236 mencoder_exit(1,NULL);
1238 if (sh_audio && mux_a->codec == ACODEC_COPY) {
1239 if (opts.playback_speed != 1.0) mp_tmsg(MSGT_CPLAYER, MSGL_WARN, NoSpeedWithFrameCopy);
1240 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1241 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
1242 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
1243 if (sh_audio->wf) {
1244 if ((mux_a->wf->wFormatTag != sh_audio->wf->wFormatTag) ||
1245 (mux_a->wf->nChannels != sh_audio->wf->nChannels) ||
1246 (mux_a->wf->nSamplesPerSec != sh_audio->wf->nSamplesPerSec * opts.playback_speed))
1248 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1249 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * opts.playback_speed),
1250 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1251 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll files must have identical audio codec and format for -oac copy.\n");
1252 mencoder_exit(1,NULL);
1254 } else {
1255 if ((mux_a->wf->wFormatTag != sh_audio->format) ||
1256 (mux_a->wf->nChannels != sh_audio->channels) ||
1257 (mux_a->wf->nSamplesPerSec != sh_audio->samplerate * opts.playback_speed))
1259 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1260 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * opts.playback_speed),
1261 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1262 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll files must have identical audio codec and format for -oac copy.\n");
1263 mencoder_exit(1,NULL);
1266 } else if (sh_audio) {
1267 int out_srate = mux_a->wf->nSamplesPerSec;
1268 int out_channels = mux_a->wf->nChannels;
1269 int out_format = aencoder->input_format;
1270 if (!init_audio_filters(sh_audio, new_srate,
1271 &out_srate, &out_channels,
1272 &out_format)) {
1273 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Couldn't find matching filter/ao format!\n");
1274 mencoder_exit(1, NULL);
1276 mux_a->wf->nSamplesPerSec = out_srate;
1277 mux_a->wf->nChannels = out_channels;
1281 if (seek_to_sec) {
1282 demux_seek(demuxer, seek_to_sec, audio_delay, 1);
1283 // there is 2 way to handle the -ss option in 3-pass mode:
1284 // > 1. do the first pass for the whole file, and use -ss for 2nd/3rd pases only
1285 // > 2. do all the 3 passes with the same -ss value
1286 // this line enables behaviour 1. (and kills 2. at the same time):
1287 // if(demuxer2) demux_seek(demuxer2, d, audio_delay, 1);
1290 if (out_file_format == MUXER_TYPE_MPEG)
1292 if (audio_preload > 0.4) {
1293 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Limiting audio preload to 0.4s.\n");
1294 audio_preload = 0.4;
1296 if (audio_density < 4) {
1297 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Increasing audio density to 4.\n");
1298 audio_density = 4;
1302 if(file_format == DEMUXER_TYPE_TV)
1304 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Forcing audio preload to 0, max pts correction to 0.\n");
1305 audio_preload = 0.0;
1306 default_max_pts_correction = 0;
1309 play_n_frames=play_n_frames_mf;
1310 if (curfile && end_at.type == END_AT_TIME) end_at.pos += mux_v->timer;
1312 if (edl_records) free_edl(edl_records);
1313 next_edl_record = edl_records = NULL;
1314 edl_muted = 0;
1315 edl_seeking = 1;
1316 if (edl_filename) {
1317 next_edl_record = edl_records = edl_parse_file();
1320 if (sh_audio && audio_delay != 0.) fixdelay(d_video, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1322 while(!at_eof){
1324 int blit_frame=0;
1325 float a_pts=0;
1326 float v_pts=0;
1327 int skip_flag=0; // 1=skip -1=duplicate
1329 if((end_at.type == END_AT_SIZE && end_at.pos <= stream_tell(muxer->stream)) ||
1330 (end_at.type == END_AT_TIME && end_at.pos < mux_v->timer))
1331 break;
1333 if(play_n_frames>=0){
1334 --play_n_frames;
1335 if(play_n_frames<0) break;
1337 if(opts.chapterrange[1]>0) {
1338 int cur_chapter = demuxer_get_current_chapter(demuxer);
1339 if(cur_chapter!=-1 && cur_chapter+1>opts.chapterrange[1])
1340 break;
1343 goto_redo_edl:
1344 if (next_edl_record && sh_video && sh_video->pts >= next_edl_record->start_sec) {
1345 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1346 float last_pos = d_video->pts;
1347 int result;
1348 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop [%f], length [%f]\n",
1349 next_edl_record->start_sec, next_edl_record->stop_sec, next_edl_record->length_sec);
1351 result = edl_seek(next_edl_record, demuxer, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1353 if (result == 2) { at_eof=1; break; } // EOF
1354 else if (result == 0) edl_seeking = 0; // no seeking
1355 else { // sucess
1356 edl_muted = 0;
1357 if (last_pos >= sh_video->pts) {
1358 // backwards seek detected!! Forget about this EDL skip altogether.
1359 next_edl_record = next_edl_record->next;
1361 else for (next_edl_record = edl_records; next_edl_record; next_edl_record = next_edl_record->next) {
1362 /* note the use of stop_sec,
1363 meaning if by some magical way we landed in the MIDDLE of a censored area,
1364 in the next loop it will jump out of it.
1366 if (next_edl_record->stop_sec > sh_video->pts) break; // we got to the right place.
1367 if (next_edl_record->action == EDL_MUTE) edl_muted = !edl_muted; // toggle mute each time.
1370 /* for a pedantic EDL, that doesn't show even a single
1371 frame from the "censored" area, uncomment next line. */
1372 goto goto_redo_edl;
1374 } else if (next_edl_record->action == EDL_MUTE) {
1375 edl_muted = !edl_muted; // This variable does nothing for now.
1376 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n", next_edl_record->start_sec );
1377 next_edl_record=next_edl_record->next;
1382 if(sh_audio){
1383 // get audio:
1384 while(mux_a->timer-audio_preload<mux_v->timer){
1385 float tottime;
1386 int len=0;
1388 ptimer_start = GetTimerMS();
1389 // CBR - copy 0.5 sec of audio
1390 // or until the end of video:
1391 tottime = stop_time(demuxer, mux_v);
1392 if (tottime != -1) {
1393 tottime -= mux_a->timer;
1394 if (tottime > 1./audio_density) tottime = 1./audio_density;
1396 else tottime = 1./audio_density;
1398 // let's not output more audio than necessary
1399 if (tottime <= 0) break;
1401 if(aencoder)
1403 if(mux_a->h.dwSampleSize) /* CBR */
1405 if(aencoder->set_decoded_len)
1407 len = mux_a->h.dwSampleSize*(int)(mux_a->h.dwRate*tottime);
1408 aencoder->set_decoded_len(aencoder, len);
1410 else
1411 len = aencoder->decode_buffer_size;
1413 len = dec_audio(sh_audio, aencoder->decode_buffer, len);
1414 mux_a->buffer_len += aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len,
1415 aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1416 if(mux_a->buffer_len < mux_a->wf->nBlockAlign)
1417 len = 0;
1418 else
1419 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1421 else /* VBR */
1423 int sz = 0;
1424 while(1)
1426 len = 0;
1427 if(! sz)
1428 sz = aencoder->get_frame_size(aencoder);
1429 if(sz > 0 && mux_a->buffer_len >= sz)
1431 len = sz;
1432 break;
1434 len = dec_audio(sh_audio,aencoder->decode_buffer, aencoder->decode_buffer_size);
1435 if(len <= 0)
1437 len = 0;
1438 break;
1440 len = aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len, aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1441 mux_a->buffer_len += len;
1444 if (mux_v->timer == 0) mux_a->h.dwInitialFrames++;
1446 else {
1447 if(mux_a->h.dwSampleSize){
1448 switch(mux_a->codec){
1449 case ACODEC_COPY: // copy
1450 len=mux_a->wf->nAvgBytesPerSec*tottime;
1451 len/=mux_a->h.dwSampleSize;if(len<1) len=1;
1452 len*=mux_a->h.dwSampleSize;
1453 len=demux_read_data(sh_audio->ds,mux_a->buffer,len);
1454 break;
1456 } else {
1457 // VBR - encode/copy an audio frame
1458 switch(mux_a->codec){
1459 case ACODEC_COPY: // copy
1460 len=ds_get_packet(sh_audio->ds,(unsigned char**) &mux_a->buffer);
1461 break;
1465 if(len<=0) break; // EOF?
1466 muxer_write_chunk(mux_a,len,AVIIF_KEYFRAME, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1467 if(!mux_a->h.dwSampleSize && mux_a->timer>0)
1468 mux_a->wf->nAvgBytesPerSec=0.5f+(double)mux_a->size/mux_a->timer; // avg bps (VBR)
1469 if(mux_a->buffer_len>=len){
1470 mux_a->buffer_len-=len;
1471 fast_memcpy(mux_a->buffer,mux_a->buffer+len,mux_a->buffer_len);
1475 audiosamples++;
1476 audiorate+= (GetTimerMS() - ptimer_start);
1481 // get video frame!
1483 if (!frame_data.already_read) {
1484 frame_data.in_size=video_read_frame(sh_video,&frame_data.frame_time,&frame_data.start,force_fps);
1485 sh_video->timer+=frame_data.frame_time;
1487 frame_data.frame_time /= opts.playback_speed;
1488 if(frame_data.in_size<0){ at_eof=1; break; }
1489 ++decoded_frameno;
1491 v_timer_corr-=frame_data.frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate;
1493 if(demuxer2){ // 3-pass encoding, read control file (frameno.avi)
1494 // find our frame:
1495 while(next_frameno<decoded_frameno){
1496 int* start;
1497 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1498 if(len<0){ at_eof=1;break;}
1499 if(len==0) --skip_flag; else // duplicate
1500 if(len==4) next_frameno=start[0];
1502 if(at_eof) break;
1503 skip_flag=next_frameno-decoded_frameno;
1504 // find next frame:
1505 while(next_frameno<=decoded_frameno){
1506 int* start;
1507 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1508 if(len<0){ at_eof=1;break;}
1509 if(len==0) --skip_flag; else // duplicate
1510 if(len==4) next_frameno=start[0];
1512 } else {
1514 // check frame duplicate/drop:
1516 float mux_frametime = (float)mux_v->h.dwScale/mux_v->h.dwRate;
1518 if (v_timer_corr >= mux_frametime && (skip_limit<0 || skip_flag < skip_limit)) {
1519 v_timer_corr-=mux_frametime;
1520 ++skip_flag; // skip
1522 while (v_timer_corr <= -mux_frametime && (skip_limit<0 || -skip_flag < skip_limit)) {
1523 v_timer_corr+=mux_frametime;
1524 --skip_flag; // dup
1527 // either v_pts_corr is big, more than 2 times framerate, then we follow its advice,
1528 // or, it cancels out v_timer_corr, in which case be happy and do nothing.
1530 while ((v_pts_corr <= -mux_frametime && skip_flag > 0) || (v_pts_corr <= -2*mux_frametime)) {
1531 v_pts_corr+=mux_frametime;
1532 --skip_flag; // dup
1534 if ((v_pts_corr >= mux_frametime && skip_flag < 0) || (v_pts_corr >= 2*mux_frametime)) {
1535 if (skip_flag<=0) { // we can't skip more than 1 frame now
1536 v_pts_corr-=mux_frametime;
1537 ++skip_flag; // skip
1541 } // demuxer2
1543 ptimer_start = GetTimerMS();
1545 switch(mux_v->codec){
1546 case VCODEC_COPY:
1547 mux_v->buffer=frame_data.start;
1548 if(skip_flag<=0) muxer_write_chunk(mux_v,frame_data.in_size,(sh_video->ds->flags&1)?AVIIF_KEYFRAME:0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1549 break;
1550 case VCODEC_FRAMENO:
1551 mux_v->buffer=(unsigned char *)&decoded_frameno; // tricky
1552 if(skip_flag<=0) muxer_write_chunk(mux_v,sizeof(int),AVIIF_KEYFRAME, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1553 break;
1554 default:
1555 // decode_video will callback down to ve_*.c encoders, through the video filters
1556 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ, osd);
1557 {void *decoded_frame = decode_video(sh_video,frame_data.start,frame_data.in_size,
1558 skip_flag>0 && (!sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) != CONTROL_TRUE), MP_NOPTS_VALUE);
1559 blit_frame = decoded_frame && filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE);
1560 if (blit_frame) {
1561 struct vf_instance *vf = sh_video->vfilter;
1562 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
1563 vf->control(vf, VFCTRL_DRAW_OSD, osd);
1567 if (sh_video->vf_initialized < 0) mencoder_exit(1, NULL);
1569 if(!blit_frame){
1570 if (play_n_frames >= 0)
1571 play_n_frames++;
1572 badframes++;
1573 if(skip_flag<=0){
1574 // unwanted skipping of a frame, what to do?
1575 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1576 #if 0
1577 // Old code apparently made under the assumption that !blit_frame means
1578 // decoding failed due to corruption or something.. but duplication and
1579 // skipping of frames should be entirely disabled when skip_limit==0,
1580 // and must be in order for many filters to work with -noskip.
1581 // Eventually this entire block should probably be removed.
1582 if(skip_limit==0){
1583 // skipping not allowed -> write empty frame:
1584 if (!encode_duplicates || !sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1585 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1586 } else {
1587 // skipping allowed -> skip it and distriubute timer error:
1588 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1590 #endif
1595 videosamples++;
1596 videorate+=(GetTimerMS() - ptimer_start);
1598 if(skip_flag<0){
1599 // duplicate frame
1600 if(!quiet) mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\n%d duplicate frame(s)!\n",-skip_flag);
1601 while(skip_flag<0){
1602 duplicatedframes++;
1603 if (!encode_duplicates || !sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1604 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1605 ++skip_flag;
1607 } else
1608 if(skip_flag>0){
1609 // skip frame
1610 if(!quiet) mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\nSkipping frame!\n");
1611 skippedframes++;
1612 --skip_flag;
1615 if(sh_audio && !demuxer2){
1616 float AV_delay,x;
1617 // A-V sync!
1618 #if 0
1619 if(pts_from_bps){
1620 unsigned int samples=(sh_audio->audio.dwSampleSize)?
1621 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
1622 (d_audio->block_no); // <- used for VBR audio
1623 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
1624 delay_corrected=1;
1625 } else
1626 #endif
1628 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
1629 a_pts=d_audio->pts;
1630 if(!delay_corrected) if(a_pts) delay_corrected=1;
1631 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1633 v_pts=sh_video ? sh_video->pts : d_video->pts;
1634 // av = compensated (with out buffering delay) A-V diff
1635 AV_delay=(a_pts-v_pts);
1636 AV_delay-=audio_delay;
1637 AV_delay /= opts.playback_speed;
1638 AV_delay-=mux_a->timer-(mux_v->timer-(v_timer_corr+v_pts_corr));
1639 // adjust for encoder delays
1640 AV_delay -= (float) mux_a->encoder_delay * mux_a->h.dwScale/mux_a->h.dwRate;
1641 AV_delay += (float) mux_v->encoder_delay * mux_v->h.dwScale/mux_v->h.dwRate;
1642 // compensate input video timer by av:
1643 x=AV_delay*0.1f;
1644 if(x<-max_pts_correction) x=-max_pts_correction; else
1645 if(x> max_pts_correction) x= max_pts_correction;
1646 if(default_max_pts_correction>=0)
1647 max_pts_correction=default_max_pts_correction*opts.playback_speed;
1648 else
1649 max_pts_correction=sh_video->frametime*0.10 *opts.playback_speed; // +-10% of time
1650 // sh_video->timer-=x;
1651 c_total+=x;
1652 v_pts_corr+=x;
1655 { float t=(GetTimerMS()-timer_start)*0.001f;
1656 float len=(demuxer->movi_end-demuxer->movi_start);
1657 off_t pos = demuxer->filepos >= 0 ? demuxer->filepos : stream_tell(demuxer->stream);
1658 float p=len>1000 ? (float)(pos-demuxer->movi_start) / len :
1659 (demuxer_get_percent_pos(demuxer) / 100.0);
1660 #if 0
1661 if(!len && sh_audio && sh_audio->audio.dwLength>100){
1662 p=(sh_audio->audio.dwSampleSize? ds_tell(sh_audio->ds)/sh_audio->audio.dwSampleSize : sh_audio->ds->block_no)
1663 / (float)(sh_audio->audio.dwLength);
1665 #endif
1666 if(!quiet) {
1667 if( mp_msg_test(MSGT_STATUSLINE,MSGL_V) ) {
1668 mp_msg(MSGT_STATUSLINE,MSGL_STATUS,"Pos:%6.1fs %6df (%2d%%) %3dfps Trem:%4dmin %3dmb A-V:%5.3f [%d:%d] A/Vms %d/%d D/B/S %d/%d/%d \r",
1669 mux_v->timer, decoded_frameno, (int)(p*100),
1670 (t>1) ? (int)(decoded_frameno/t+0.5) : 0,
1671 (p>0.001) ? (int)((t/p-t)/60) : 0,
1672 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1673 v_pts_corr,
1674 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1675 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0,
1676 audiorate/audiosamples, videorate/videosamples,
1677 duplicatedframes, badframes, skippedframes
1679 } else
1680 mp_msg(MSGT_STATUSLINE,MSGL_STATUS,"Pos:%6.1fs %6df (%2d%%) %5.2ffps Trem:%4dmin %3dmb A-V:%5.3f [%d:%d]\r",
1681 mux_v->timer, decoded_frameno, (int)(p*100),
1682 (t>1) ? (float)(decoded_frameno/t) : 0,
1683 (p>0.001) ? (int)((t/p-t)/60) : 0,
1684 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1685 v_pts_corr,
1686 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1687 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0
1691 fflush(stdout);
1693 #ifdef CONFIG_DVDREAD
1694 // DVD sub:
1695 if(vobsub_writer){
1696 unsigned char* packet=NULL;
1697 int len;
1698 while((len=ds_get_packet_sub(d_dvdsub,&packet))>0){
1699 mp_msg(MSGT_MENCODER,MSGL_V,"\rDVD sub: len=%d v_pts=%5.3f s_pts=%5.3f \n",len,sh_video->pts,d_dvdsub->pts);
1700 vobsub_out_output(vobsub_writer,packet,len,mux_v->timer + d_dvdsub->pts - sh_video->pts);
1703 else
1704 #endif
1705 update_subtitles(NULL, &opts, sh_video, sh_video->pts, 0, d_dvdsub, 0);
1707 frame_data = (s_frame_data){ .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
1709 #if 0
1710 if(ferror(muxer_f)) {
1711 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n", out_filename);
1712 mencoder_exit(1, NULL);
1714 #endif
1716 } // while(!at_eof)
1718 if (!interrupted && filelist[++curfile].name != 0) {
1719 if (sh_video && sh_video->vfilter) { // Before uniniting sh_video and the filter chain, break apart the VE.
1720 vf_instance_t * ve; // this will be the filter right before the ve.
1721 for (ve = sh_video->vfilter; ve->next && ve->next->next; ve = ve->next);
1723 if (ve->next) ve->next = NULL; // I'm telling the last filter, before the VE, there is nothing after it
1724 else sh_video->vfilter = NULL; // There is no chain except the VE.
1727 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1728 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1729 if(demuxer) free_demuxer(demuxer);
1730 if(stream) free_stream(stream); // kill cache thread
1732 at_eof = 0;
1734 m_config_pop(mconfig);
1735 goto play_next_file;
1738 /* Emit the remaining frames in the video system */
1739 /*TODO emit frmaes delayed by decoder lag*/
1740 if(sh_video && sh_video->vfilter){
1741 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nFlushing video frames.\n");
1742 if (!sh_video->vfilter->fmt.have_configured)
1743 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Filters have not been configured! Empty file?\n");
1744 else
1745 sh_video->vfilter->control(sh_video->vfilter,
1746 VFCTRL_FLUSH_FRAMES, 0);
1749 if(aencoder)
1750 if(aencoder->fixup)
1751 aencoder->fixup(aencoder);
1753 if (muxer->cont_write_index) muxer_write_index(muxer);
1754 muxer_f_size=stream_tell(muxer->stream);
1755 stream_seek(muxer->stream,0);
1756 if (muxer->cont_write_header) muxer_write_header(muxer); // update header
1757 #if 0
1758 if(ferror(muxer_f) || fclose(muxer_f) != 0) {
1759 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n", out_filename);
1760 mencoder_exit(1, NULL);
1762 #endif
1763 if(vobsub_writer)
1764 vobsub_out_close(vobsub_writer);
1766 if(out_video_codec==VCODEC_FRAMENO && mux_v->timer>100){
1767 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Recommended video bitrate for %s CD: %d\n","650MB",(int)((650*1024*1024-muxer_f_size)/mux_v->timer/125));
1768 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Recommended video bitrate for %s CD: %d\n","700MB",(int)((700*1024*1024-muxer_f_size)/mux_v->timer/125));
1769 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Recommended video bitrate for %s CD: %d\n","800MB",(int)((800*1024*1024-muxer_f_size)/mux_v->timer/125));
1770 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Recommended video bitrate for %s CD: %d\n","2 x 650MB",(int)((2*650*1024*1024-muxer_f_size)/mux_v->timer/125));
1771 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Recommended video bitrate for %s CD: %d\n","2 x 700MB",(int)((2*700*1024*1024-muxer_f_size)/mux_v->timer/125));
1772 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Recommended video bitrate for %s CD: %d\n","2 x 800MB",(int)((2*800*1024*1024-muxer_f_size)/mux_v->timer/125));
1775 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nVideo stream: %8.3f kbit/s (%d B/s) size: %"PRIu64" bytes %5.3f secs %d frames\n",
1776 (float)(mux_v->size/mux_v->timer*8.0f/1000.0f), (int)(mux_v->size/mux_v->timer), (uint64_t)mux_v->size, (float)mux_v->timer, decoded_frameno);
1777 if(sh_audio)
1778 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nAudio stream: %8.3f kbit/s (%d B/s) size: %"PRIu64" bytes %5.3f secs\n",
1779 (float)(mux_a->size/mux_a->timer*8.0f/1000.0f), (int)(mux_a->size/mux_a->timer), (uint64_t)mux_a->size, (float)mux_a->timer);
1781 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1782 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1783 if(demuxer) free_demuxer(demuxer);
1784 if(stream) free_stream(stream); // kill cache thread
1786 return interrupted;