configure: fix disabling of (x)mga without swscale internals
[mplayer/glamo.git] / mencoder.c
blob47f9979b0e7c7ccf05755d4dfc8606677259b073
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 static char* spudec_ifo=NULL;
130 static char** audio_codec_list=NULL; // override audio codec
131 static char** video_codec_list=NULL; // override video codec
132 static char** audio_fm_list=NULL; // override audio codec family
133 static char** video_fm_list=NULL; // override video codec family
134 extern char *demuxer_name; // override demuxer
135 extern char *audio_demuxer_name; // override audio demuxer
136 extern char *sub_demuxer_name; // override sub demuxer
138 static int out_audio_codec=-1;
139 static int out_video_codec=-1;
141 int out_file_format=MUXER_TYPE_AVI; // default to AVI
142 int quiet=0;
143 double video_time_usage=0;
144 double vout_time_usage=0;
145 double max_video_time_usage=0;
146 double max_vout_time_usage=0;
147 double cur_video_time_usage=0;
148 double cur_vout_time_usage=0;
149 int benchmark=0;
151 #include "osdep/priority.h"
153 // A-V sync:
154 int delay_corrected=1;
155 static float default_max_pts_correction=-1;//0.01f;
156 static float max_pts_correction=0;//default_max_pts_correction;
157 static float c_total=0;
159 static float audio_preload=0.5;
160 static float audio_delay_fix=0.0;
161 static float audio_delay=0.0;
162 static int ignore_start=0;
163 static int audio_density=2;
165 double force_fps=0;
166 static double force_ofps=0; // set to 24 for inverse telecine
167 static int skip_limit=-1;
169 static int force_srate=0;
170 static int audio_output_format=0;
172 char *vobsub_out=NULL;
173 unsigned int vobsub_out_index=0;
174 char *vobsub_out_id=NULL;
176 char* out_filename=NULL;
178 char *force_fourcc=NULL;
179 int force_audiofmttag=-1;
181 char* passtmpfile="divx2pass.log";
183 static int play_n_frames=-1;
184 static int play_n_frames_mf=-1;
186 #include "libvo/font_load.h"
187 #include "libvo/sub.h"
189 // sub:
190 char *font_name=NULL;
191 char *sub_font_name=NULL;
192 extern int font_fontconfig;
193 float font_factor=0.75;
194 char **sub_name=NULL;
195 float sub_delay=0;
196 float sub_fps=0;
197 int sub_auto = 0;
198 int subcc_enabled=0;
199 int suboverlap_enabled = 1;
201 int auto_expand=1;
202 int encode_duplicates=1;
204 // infos are empty by default
205 char *info_name=NULL;
206 char *info_artist=NULL;
207 char *info_genre=NULL;
208 char *info_subject=NULL;
209 char *info_copyright=NULL;
210 char *info_sourceform=NULL;
211 char *info_comment=NULL;
213 // Needed by libmpcodecs vf_vo.c
214 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
215 uint32_t d_width, uint32_t d_height, uint32_t flags,
216 char *title, uint32_t format) { abort(); }
217 int vo_control(struct vo *vo, uint32_t request, void *data) { abort(); }
218 void vo_seek_reset(struct vo *vo) { abort(); }
219 int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts) { abort(); }
220 int vo_draw_frame(struct vo *vo, uint8_t *src[]) { abort(); }
221 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y) { abort(); }
222 void vo_draw_osd(struct vo *vo, struct osd_state *osd) { abort(); }
223 void vo_flip_page(struct vo *vo, uint32_t pts_us, int duration) { abort(); }
224 void vo_check_events(struct vo *vo) { abort(); }
226 // Needed by getch2
227 struct mp_fifo;
228 void mplayer_put_key(struct mp_fifo *fifo, int code)
232 #include "ass_mp.h"
233 char *current_module;
234 #include "mpcommon.h"
236 // Needed by mpcommon.c
237 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs) {
238 vo_sub = subs;
239 vo_osd_changed(OSDTYPE_SUBTITLE);
242 //-------------------------- config stuff:
244 m_config_t* mconfig;
246 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
248 static int cfg_include(m_option_t *conf, char *filename){
249 return m_config_parse_config_file(mconfig, filename);
252 static double seek_to_sec;
253 static off_t seek_to_byte=0;
255 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
257 static char * frameno_filename=NULL;
259 typedef struct {
260 unsigned char* start;
261 int in_size;
262 float frame_time;
263 int already_read;
264 } s_frame_data;
266 #include "edl.h"
267 static edl_record_ptr edl_records = NULL; ///< EDL entries memory area
268 static edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
269 static short edl_muted; ///< Stores whether EDL is currently in muted mode.
270 static short edl_seeking; ///< When non-zero, stream is seekable.
271 static short edl_seek_type; ///< When non-zero, frames are discarded instead of seeking.
273 #include "cfg-mencoder.h"
275 #include "spudec.h"
276 #include "vobsub.h"
278 #include "libao2/audio_out.h"
279 /* FIXME */
280 static void mencoder_exit(int level, const char *how)
282 if (how)
283 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nExiting... (%s)\n", how);
284 else
285 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nExiting...\n");
287 exit(level);
290 static void parse_cfgfiles( m_config_t* conf )
292 char *conffile;
293 if (!disable_system_conf &&
294 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mencoder.conf") < 0)
295 mencoder_exit(1,_("config file error"));
297 if (!disable_user_conf) {
298 if ((conffile = get_path("mencoder.conf")) == NULL) {
299 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
300 } else {
301 if (m_config_parse_config_file(conf, conffile) < 0)
302 mencoder_exit(1,_("config file error"));
303 free(conffile);
309 //---------------------------------------------------------------------------
311 static int dec_audio(sh_audio_t *sh_audio,unsigned char* buffer,int total){
312 int size=0;
313 int at_eof=0;
314 while(size<total && !at_eof){
315 int len=total-size;
316 if (decode_audio(sh_audio, len) < 0) at_eof=1;
317 if(len>sh_audio->a_out_buffer_len) len=sh_audio->a_out_buffer_len;
318 fast_memcpy(buffer+size,sh_audio->a_out_buffer,len);
319 sh_audio->a_out_buffer_len-=len; size+=len;
320 if(sh_audio->a_out_buffer_len>0)
321 fast_memcpy(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[len],sh_audio->a_out_buffer_len);
323 return size;
326 //---------------------------------------------------------------------------
328 static volatile int at_eof=0;
329 static volatile int interrupted=0;
331 static void exit_sighandler(int x){
332 at_eof=1;
333 interrupted=2; /* 1 means error */
336 static muxer_t* muxer=NULL;
338 static void add_subtitles(char *filename, float fps, int silent)
340 sub_data *subd;
341 #ifdef CONFIG_ASS
342 ASS_Track *asst = 0;
343 #endif
345 if (!filename) return;
347 subd = sub_read_file(filename, fps);
348 #ifdef CONFIG_ASS
349 if (opts.ass_enabled)
350 #ifdef CONFIG_ICONV
351 asst = ass_read_file(ass_library, filename, sub_cp);
352 #else
353 asst = ass_read_file(ass_library, filename, 0);
354 #endif
355 if (opts.ass_enabled && subd && !asst)
356 asst = ass_read_subdata(ass_library, subd, fps);
358 if (!asst && !subd && !silent)
359 #else
360 if (!subd && !silent)
361 #endif
362 mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "Cannot load subtitles: %s\n",
363 filename_recode(filename));
365 #ifdef CONFIG_ASS
366 if (!asst && !subd) return;
367 ass_track = asst;
368 #else
369 if (!subd) return;
370 #endif
371 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
372 filename_recode(filename));
373 subdata = subd;
376 /* This function returns the absolute time for which MEncoder will switch files
377 * or move in the file so audio can be cut correctly. -1 if there is no limit. */
378 static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v)
380 float timeleft = -1;
381 if (play_n_frames >= 0) timeleft = mux_v->timer + play_n_frames * (double)(mux_v->h.dwScale) / mux_v->h.dwRate;
382 if (end_at.type == END_AT_TIME && (timeleft > end_at.pos || timeleft == -1)) timeleft = end_at.pos;
383 if (next_edl_record && demuxer && demuxer->video) { // everything is OK to be checked
384 float tmp = mux_v->timer + next_edl_record->start_sec - demuxer->video->pts;
385 if (timeleft == -1 || timeleft > tmp) {
386 // There's less time in EDL than what we already know
387 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
388 timeleft = tmp;
389 } else if (next_edl_record->action == EDL_MUTE) {
390 //timeleft = next_edl_record->start_sec - demuxer->video->pts;
391 // For the moment (and probably forever) EDL mute doesn't work in MEncoder
395 return timeleft;
398 /// Returns a_pts
399 static float calc_a_pts(demux_stream_t *d_audio)
401 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
402 float a_pts = 0.;
403 if (sh_audio)
404 a_pts = d_audio->pts + (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
405 return a_pts;
408 /** \brief Seeks audio forward to pts by dumping audio packets
409 * \return The current audio pts. */
410 static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a)
412 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
413 int samplesize, avg;
414 float a_pts = calc_a_pts(d_audio);
416 if (!sh_audio) return a_pts;
418 if (sh_audio->audio.dwScale) samplesize = sh_audio->audio.dwSampleSize;
419 else samplesize = (sh_audio->wf ? sh_audio->wf->nBlockAlign : 1);
420 avg = (sh_audio->wf ? sh_audio->wf->nAvgBytesPerSec : sh_audio->i_bps);
422 // after a demux_seek, a_pts will be zero until you read some audio.
423 // carefully checking if a_pts is truely correct by reading tiniest amount of data possible.
424 if (pts > a_pts && a_pts == 0.0 && samplesize) {
425 if (demux_read_data(sh_audio->ds,mux_a->buffer,samplesize) <= 0) return a_pts; // EOF
426 a_pts = calc_a_pts(d_audio);
429 while (pts > a_pts) {
430 int len;
431 if (samplesize) {
432 len = avg * (pts - a_pts > 0.5 ? 0.5 : pts - a_pts);
433 len/= samplesize; if(len<1) len=1;
434 len*= samplesize;
435 len = demux_read_data(sh_audio->ds,mux_a->buffer,len);
436 } else {
437 unsigned char * crap;
438 len = ds_get_packet(sh_audio->ds, &crap);
440 if (len <= 0) break; // EOF of audio.
441 a_pts = calc_a_pts(d_audio);
443 return a_pts;
446 /** \brief Seeks slowly by dumping frames.
447 * \return 1 for success, 2 for EOF. */
448 static int slowseek(float end_pts, demux_stream_t *d_video,
449 demux_stream_t *d_audio, muxer_stream_t *mux_a,
450 s_frame_data *frame_data, int framecopy, int print_info)
452 sh_video_t * sh_video = d_video->sh;
453 vf_instance_t * vfilter = sh_video ? sh_video->vfilter : NULL;
454 int done = 0;
456 while (!interrupted) {
457 float a_pts = 0.;
459 if (!frame_data->already_read) { // when called after fixdelay, a frame is already read
460 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
461 if(frame_data->in_size<0) return 2;
462 sh_video->timer += frame_data->frame_time;
464 frame_data->already_read = 0;
466 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
468 if (done) {
469 // wait for keyframe in case of -ovc copy
470 if (!framecopy || (sh_video->ds->flags & 1)) {
471 frame_data->already_read = 1;
472 return 1;
475 if (sh_video->pts >= end_pts) done = 1;
477 if (vfilter) {
478 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
479 osd);
480 int softskip = (vfilter->control(vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) == CONTROL_TRUE);
481 void *decoded_frame = decode_video(sh_video, frame_data->start, frame_data->in_size, !softskip, MP_NOPTS_VALUE);
482 if (decoded_frame)
483 if (filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE)) {
484 struct vf_instance *vf = sh_video->vfilter;
485 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
486 vf->control(vf, VFCTRL_DRAW_OSD, osd);
490 if (print_info) mp_tmsg(MSGT_MENCODER, MSGL_STATUS,
491 "EDL SKIP: Start: %.2f End: %.2f Current: V: %.2f A: %.2f \r",
492 next_edl_record->start_sec, next_edl_record->stop_sec,
493 sh_video->pts, a_pts);
495 if (interrupted) return 2;
496 return 1;
499 /// Deletes audio or video as told by -delay to sync
500 static void fixdelay(demux_stream_t *d_video, demux_stream_t *d_audio,
501 muxer_stream_t *mux_a, s_frame_data *frame_data,
502 int framecopy)
504 // TODO: Find a way to encode silence instead of deleting video
505 sh_video_t * sh_video = d_video->sh;
506 float a_pts;
508 // demux_seek has a weirdness that sh_video->pts is meaningless,
509 // until a single frame is read... Same for audio actually too.
510 // Reading one frame, and keeping it.
511 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
512 if(frame_data->in_size<0) return;
513 sh_video->timer += frame_data->frame_time;
514 frame_data->already_read = 1;
516 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
518 if (audio_delay > 0) return;
519 else if (sh_video->pts - frame_data->frame_time + audio_delay >= a_pts) return;
521 slowseek(a_pts - audio_delay, d_video, d_audio, mux_a, frame_data, framecopy, 0);
524 /** \brief Seeks for EDL
525 * \return 1 for success, 0 for failure, 2 for EOF. */
526 static int edl_seek(edl_record_ptr next_edl_record, demuxer_t *demuxer,
527 demux_stream_t *d_audio, muxer_stream_t *mux_a,
528 s_frame_data *frame_data, int framecopy)
530 sh_video_t * sh_video = demuxer->video ? demuxer->video->sh : NULL;
532 if (!sh_video) return 0;
533 if (sh_video->pts >= next_edl_record->stop_sec) return 1; // nothing to do...
535 if (!edl_seek_type) {
536 if(demux_seek(demuxer, next_edl_record->stop_sec - sh_video->pts, audio_delay, 0)){
537 sh_video->pts = demuxer->video->pts;
538 //if (vo_vobsub) vobsub_seek(vo_vobsub,sh_video->pts);
539 resync_video_stream(sh_video);
540 //if(vo_spudec) spudec_reset(vo_spudec);
541 if (audio_delay != 0.0) fixdelay(demuxer->video, d_audio, mux_a, frame_data, framecopy);
542 return 1;
544 // non-seekable stream.
545 return 0;
548 // slow seek, read every frame.
550 return slowseek(next_edl_record->stop_sec, demuxer->video, d_audio, mux_a, frame_data, framecopy, 1);
554 int main(int argc,char* argv[]){
556 stream_t* stream=NULL;
557 stream_t* ostream=NULL;
558 demuxer_t* demuxer=NULL;
559 stream_t* stream2=NULL;
560 demuxer_t* demuxer2=NULL;
561 demux_stream_t *d_audio=NULL;
562 demux_stream_t *d_video=NULL;
563 demux_stream_t *d_dvdsub=NULL;
564 sh_audio_t *sh_audio=NULL;
565 sh_video_t *sh_video=NULL;
566 int file_format=DEMUXER_TYPE_UNKNOWN;
567 int i=DEMUXER_TYPE_UNKNOWN;
568 void *vobsub_writer=NULL;
569 s_frame_data frame_data = { .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
571 uint32_t ptimer_start;
572 uint32_t audiorate=0;
573 uint32_t videorate=0;
574 uint32_t audiosamples=1;
575 uint32_t videosamples=1;
576 uint32_t skippedframes=0;
577 uint32_t duplicatedframes=0;
578 uint32_t badframes=0;
580 muxer_stream_t* mux_a=NULL;
581 muxer_stream_t* mux_v=NULL;
582 off_t muxer_f_size=0;
584 double v_pts_corr=0;
585 double v_timer_corr=0;
587 m_entry_t* filelist = NULL;
588 char* filename=NULL;
590 int decoded_frameno=0;
591 int next_frameno=-1;
592 int curfile=0;
593 int new_srate=0;
595 unsigned int timer_start=0;
596 ao_data_t ao_data = {0,0,0,0,OUTBURST,-1,0};
598 audio_encoding_params_t aparams;
599 audio_encoder_t *aencoder = NULL;
601 mp_msg_init();
602 set_av_log_callback();
604 // Create the config context and register the options
605 set_default_mencoder_options(&opts);
606 mconfig = m_config_new(&opts, cfg_include);
607 m_config_register_options(mconfig,mencoder_opts);
609 // Preparse the command line
610 m_config_preparse_command_line(mconfig,argc,argv);
612 print_version("MEncoder");
614 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
615 set_path_env();
616 #endif
618 InitTimer();
620 // check codec.conf
621 if(!codecs_file || !parse_codec_cfg(codecs_file)){
622 if(!parse_codec_cfg(get_path("codecs.conf"))){
623 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
624 if(!parse_codec_cfg(NULL)){
625 mencoder_exit(1,NULL);
627 mp_tmsg(MSGT_MENCODER,MSGL_V,"Using built-in default codecs.conf.\n");
632 parse_cfgfiles(mconfig);
633 filelist = m_config_parse_me_command_line(mconfig, argc, argv);
634 if(!filelist) mencoder_exit(1, _("error parsing command line"));
637 char *extension;
639 if (!out_filename) mencoder_exit(1,_("No output file specified, please see the -o option."));
640 extension=strrchr(out_filename,'.');
641 if (extension != NULL && strlen(extension) > 3 && strlen(extension) < 6)
643 extension++;
645 switch (out_file_format)
647 case MUXER_TYPE_AVI:
648 if (strcasecmp(extension,"avi"))
649 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\nWARNING: OUTPUT FILE FORMAT IS _AVI_. See -of help.\n");
650 break;
652 case MUXER_TYPE_MPEG:
653 if (strcasecmp(extension,"mpg") &&
654 strcasecmp(extension,"mpeg") &&
655 strcasecmp(extension,"vob"))
656 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\nWARNING: OUTPUT FILE FORMAT IS _MPEG_. See -of help.\n");
657 break;
661 /* Display what configure line was used */
662 mp_msg(MSGT_MENCODER, MSGL_V, "Configuration: " CONFIGURATION "\n");
664 #define FormatNotRecognized _("============ Sorry, this file format is not recognized/supported =============\n"\
665 "=== If this file is an AVI, ASF or MPEG stream, please contact the author! ===\n")
668 if (frameno_filename) {
669 stream2=open_stream(frameno_filename,0,&i);
670 if(stream2){
671 demuxer2=demux_open(&opts, stream2,DEMUXER_TYPE_AVI,-1,-1,-2,NULL);
672 if(demuxer2) mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Using pass3 control file: %s\n", frameno_filename);
673 else mp_tmsg(MSGT_DEMUXER,MSGL_ERR, FormatNotRecognized);
677 #ifdef CONFIG_PRIORITY
678 set_priority();
679 #endif
681 if (codec_path)
682 set_codec_path(codec_path);
684 // check font
685 #ifdef CONFIG_FREETYPE
686 init_freetype();
687 #endif
688 #ifdef CONFIG_FONTCONFIG
689 if(font_fontconfig <= 0)
691 #endif
692 #ifdef CONFIG_BITMAP_FONT
693 if(font_name){
694 vo_font=read_font_desc(font_name,font_factor,verbose>1);
695 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",font_name);
696 } else {
697 // try default:
698 vo_font=read_font_desc(get_path("font/font.desc"),font_factor,verbose>1);
699 if(!vo_font)
700 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
702 #endif
703 #ifdef CONFIG_FONTCONFIG
705 #endif
707 osd = osd_create();
709 /* HACK, for some weird reason, push() has to be called twice,
710 otherwise options are not saved correctly */
711 m_config_push(mconfig);
712 play_next_file:
713 m_config_push(mconfig);
714 m_entry_set_options(mconfig,&filelist[curfile]);
715 filename = filelist[curfile].name;
717 #ifdef CONFIG_ASS
718 ass_library = ass_init();
719 #endif
721 if(!filename){
722 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "\nFilename missing.\n\n");
723 mencoder_exit(1,NULL);
725 stream=open_stream(filename,0,&file_format);
727 if(!stream){
728 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Cannot open file/device.\n");
729 mencoder_exit(1,NULL);
732 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));
734 #ifdef CONFIG_DVDREAD
735 if(stream->type==STREAMTYPE_DVD){
736 if(opts.audio_lang && opts.audio_id==-1) opts.audio_id=dvd_aid_from_lang(stream,opts.audio_lang);
737 if(opts.sub_lang && opts.sub_id==-1) opts.sub_id=dvd_sid_from_lang(stream,opts.sub_lang);
739 #endif
741 #ifdef CONFIG_DVDNAV
742 if(stream->type==STREAMTYPE_DVDNAV){
743 if(opts.audio_lang && opts.audio_id==-1) opts.audio_id=mp_dvdnav_aid_from_lang(stream,opts.audio_lang);
744 if(opts.sub_lang && opts.sub_id==-1) opts.sub_id=mp_dvdnav_sid_from_lang(stream,opts.sub_lang);
746 #endif
748 stream->start_pos+=seek_to_byte;
750 if(stream_cache_size>0) stream_enable_cache(stream,stream_cache_size*1024,0,0);
752 if(demuxer2) opts.audio_id=-2; /* do NOT read audio packets... */
754 demuxer=demux_open(&opts, stream,file_format,opts.audio_id,opts.video_id,opts.sub_id,filename);
755 if(!demuxer){
756 mp_tmsg(MSGT_DEMUXER, MSGL_FATAL, FormatNotRecognized);
757 mp_tmsg(MSGT_DEMUXER, MSGL_FATAL, "Cannot open demuxer.\n");
758 mencoder_exit(1,NULL);
761 if (ts_prog) {
762 demux_program_t prog = { .progid = ts_prog };
763 if (demux_control(demuxer, DEMUXER_CTRL_IDENTIFY_PROGRAM, &prog) != DEMUXER_CTRL_NOTIMPL) {
764 opts.audio_id = prog.aid; // switching is handled by select_audio below
765 opts.video_id = prog.vid;
766 demuxer_switch_video(demuxer, opts.video_id);
769 select_audio(demuxer, opts.audio_id, opts.audio_lang);
771 if (opts.sub_id < -1 && opts.sub_lang)
772 opts.sub_id = demuxer_sub_track_by_lang(demuxer, opts.sub_lang);
774 if (opts.sub_id < -1)
775 opts.sub_id = demuxer_default_sub_track(demuxer);
777 for (i = 0; i < MAX_S_STREAMS; i++) {
778 sh_sub_t *sh = demuxer->s_streams[i];
779 if (sh && sh->sid == opts.sub_id) {
780 demuxer->sub->id = i;
781 demuxer->sub->sh = sh;
782 break;
786 if(opts.chapterrange[0]>1) {
787 double pts;
788 if (demuxer_seek_chapter(demuxer, opts.chapterrange[0]-1, &pts, NULL) >= 0 && pts > -1.0)
789 seek_to_sec = pts;
792 d_audio=demuxer2 ? demuxer2->audio : demuxer->audio;
793 d_video=demuxer->video;
794 d_dvdsub=demuxer->sub;
795 sh_audio=d_audio->sh;
796 sh_video=d_video->sh;
798 if(!sh_video)
800 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Video stream is mandatory!\n");
801 mencoder_exit(1,NULL);
804 if(!video_read_properties(sh_video)){
805 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Video: Cannot read properties.\n");
806 mencoder_exit(1,NULL);
809 mp_tmsg(MSGT_MENCODER,MSGL_INFO, "[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
810 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
811 sh_video->fps,sh_video->frametime
814 if(force_fps){
815 sh_video->fps=force_fps;
816 sh_video->frametime=1.0f/sh_video->fps;
817 mp_tmsg(MSGT_MENCODER,MSGL_INFO,"Input fps will be interpreted as %5.3f instead.\n", sh_video->fps);
820 if(sh_audio && out_audio_codec<0){
821 if(opts.audio_id==-2)
822 mp_tmsg(MSGT_MENCODER,MSGL_ERR,"This demuxer doesn't support -nosound yet.\n");
823 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nNo audio encoder (-oac) selected. Select one (see -oac help) or use -nosound.\n");
824 mencoder_exit(1,NULL);
826 if(sh_video && out_video_codec<0){
827 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nNo video encoder (-ovc) selected. Select one (see -ovc help).\n");
828 mencoder_exit(1,NULL);
831 if(sh_audio && (out_audio_codec || seek_to_sec || !sh_audio->wf || opts.playback_speed != 1.0)){
832 // Go through the codec.conf and find the best codec...
833 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
834 if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
835 sh_audio=d_audio->sh=NULL; // failed to init :(
837 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
840 if (sh_audio) {
841 new_srate = sh_audio->samplerate;
842 if (opts.playback_speed != 1.0) {
843 new_srate *= opts.playback_speed;
844 // limits are taken from libaf/af_resample.c
845 if (new_srate < 8000) new_srate = 8000;
846 if (new_srate > 192000) new_srate = 192000;
847 opts.playback_speed = (float)new_srate / (float)sh_audio->samplerate;
851 // set up video encoder:
853 if (!curfile) { // curfile is non zero when a second file is opened
854 if (vobsub_out) {
855 unsigned int palette[16], width, height;
856 unsigned char tmp[3] = { 0, 0, 0 };
857 if (spudec_ifo && vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, opts.sub_id, tmp) >= 0)
858 vobsub_writer = vobsub_out_open(vobsub_out, palette, sh_video->disp_w, sh_video->disp_h,
859 vobsub_out_id?vobsub_out_id:(char *)tmp, vobsub_out_index);
860 #ifdef CONFIG_DVDREAD
861 if (vobsub_writer == NULL) {
862 char tmp[3];
863 if (vobsub_out_id == NULL && stream->type == STREAMTYPE_DVD) {
864 int i;
865 dvd_priv_t *dvd = (dvd_priv_t*)stream->priv;
866 for (i = 0; i < dvd->nr_of_subtitles; ++i)
867 if (dvd->subtitles[i].id == opts.sub_id) {
868 tmp[0] = (dvd->subtitles[i].language >> 8) & 0xff;
869 tmp[1] = dvd->subtitles[i].language & 0xff;
870 tmp[2] = 0;
871 vobsub_out_id = tmp;
872 break;
875 vobsub_writer=vobsub_out_open(vobsub_out, stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
876 sh_video->disp_w, sh_video->disp_h, vobsub_out_id, vobsub_out_index);
878 #endif
880 else {
881 if (spudec_ifo) {
882 unsigned int palette[16], width, height;
883 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
884 vo_spudec=spudec_new_scaled(palette, sh_video->disp_w, sh_video->disp_h, NULL, 0);
886 #ifdef CONFIG_DVDREAD
887 if (vo_spudec==NULL) {
888 vo_spudec=spudec_new_scaled(stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
889 sh_video->disp_w, sh_video->disp_h, NULL, 0);
891 #endif
892 if (vo_spudec)
893 spudec_set_forced_subs_only(vo_spudec, forced_subs_only);
896 ostream = open_output_stream(out_filename, 0);
897 if(!ostream) {
898 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, "Cannot open output file '%s'.\n", out_filename);
899 mencoder_exit(1,NULL);
902 muxer=muxer_new_muxer(out_file_format,ostream);
903 if(!muxer) {
904 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, "Cannot initialize muxer.");
905 mencoder_exit(1,NULL);
907 #if 0
908 //disabled: it horrybly distorts filtered sound
909 if(out_file_format == MUXER_TYPE_MPEG) audio_preload = 0;
910 #endif
912 muxer->audio_delay_fix = audio_delay_fix;
914 // ============= VIDEO ===============
916 mux_v=muxer_new_stream(muxer,MUXER_TYPE_VIDEO);
918 mux_v->buffer_size=0x200000; // 2MB
919 mux_v->buffer=malloc(mux_v->buffer_size);
921 mux_v->source=sh_video;
923 mux_v->h.dwSampleSize=0; // VBR
924 #ifdef CONFIG_LIBAVCODEC
926 double fps = force_ofps?force_ofps:sh_video->fps*opts.playback_speed;
927 AVRational q= av_d2q(fps, fps*1001+2);
928 mux_v->h.dwScale= q.den;
929 mux_v->h.dwRate = q.num;
931 #else
932 mux_v->h.dwScale=10000;
933 mux_v->h.dwRate=mux_v->h.dwScale*(force_ofps?force_ofps:sh_video->fps*opts.playback_speed);
934 #endif
936 mux_v->codec=out_video_codec;
938 mux_v->bih=NULL;
940 sh_video->codec=NULL;
941 sh_video->vfilter=NULL; // fixme!
943 switch(mux_v->codec){
944 case VCODEC_COPY:
945 if (!curfile) {
946 if (sh_video->bih) {
947 mux_v->bih=malloc(sh_video->bih->biSize);
948 memcpy(mux_v->bih, sh_video->bih, sh_video->bih->biSize);
950 else
952 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
953 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
954 mux_v->bih->biWidth=sh_video->disp_w;
955 mux_v->bih->biHeight=sh_video->disp_h;
956 mux_v->bih->biCompression=sh_video->format;
957 mux_v->bih->biPlanes=1;
958 mux_v->bih->biBitCount=24; // FIXME!!!
959 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
962 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
963 mux_v->bih->biWidth, mux_v->bih->biHeight,
964 mux_v->bih->biBitCount, mux_v->bih->biCompression);
966 if (curfile) {
967 if (sh_video->bih) {
968 if ((mux_v->bih->biSize != sh_video->bih->biSize) ||
969 memcmp(mux_v->bih, sh_video->bih, sh_video->bih->biSize))
971 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
972 sh_video->bih->biWidth, sh_video->bih->biHeight,
973 sh_video->bih->biBitCount, sh_video->bih->biCompression);
974 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll video files must have identical fps, resolution, and codec for -ovc copy.\n");
975 mencoder_exit(1,NULL);
978 else {
979 if ((mux_v->bih->biWidth != sh_video->disp_w) ||
980 (mux_v->bih->biHeight != sh_video->disp_h) ||
981 (mux_v->bih->biCompression != sh_video->format)) {
982 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
983 sh_video->disp_w, sh_video->disp_w, 24, sh_video->format);
984 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll video files must have identical fps, resolution, and codec for -ovc copy.\n");
985 mencoder_exit(1,NULL);
989 break;
990 case VCODEC_FRAMENO:
991 if (!curfile) {
992 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
993 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
994 mux_v->bih->biWidth=sh_video->disp_w;
995 mux_v->bih->biHeight=sh_video->disp_h;
996 mux_v->bih->biPlanes=1;
997 mux_v->bih->biBitCount=24;
998 mux_v->bih->biCompression=mmioFOURCC('F','r','N','o');
999 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
1001 break;
1002 default: {
1003 static vf_instance_t * ve = NULL;
1004 if (!ve) {
1005 switch(mux_v->codec){
1006 case VCODEC_LIBAVCODEC:
1007 sh_video->vfilter=vf_open_encoder(&opts, NULL,"lavc",(char *)mux_v); break;
1008 case VCODEC_RAW:
1009 sh_video->vfilter=vf_open_encoder(&opts, NULL,"raw",(char *)mux_v); break;
1010 case VCODEC_VFW:
1011 sh_video->vfilter=vf_open_encoder(&opts, NULL,"vfw",(char *)mux_v); break;
1012 case VCODEC_LIBDV:
1013 sh_video->vfilter=vf_open_encoder(&opts, NULL,"libdv",(char *)mux_v); break;
1014 case VCODEC_XVID:
1015 sh_video->vfilter=vf_open_encoder(&opts, NULL,"xvid",(char *)mux_v); break;
1016 case VCODEC_QTVIDEO:
1017 sh_video->vfilter=vf_open_encoder(&opts, NULL,"qtvideo",(char *)mux_v); break;
1018 case VCODEC_NUV:
1019 sh_video->vfilter=vf_open_encoder(&opts, NULL,"nuv",(char *)mux_v); break;
1020 case VCODEC_X264:
1021 sh_video->vfilter=vf_open_encoder(&opts, NULL,"x264",(char *)mux_v); break;
1023 if(!mux_v->bih || !sh_video->vfilter){
1024 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"Failed to open the encoder.\n");
1025 mencoder_exit(1,NULL);
1027 ve = sh_video->vfilter;
1028 } else sh_video->vfilter = ve;
1029 // append 'expand' filter, it fixes stride problems and renders osd:
1030 #ifdef CONFIG_ASS
1031 if (auto_expand && !opts.ass_enabled) { /* we do not want both */
1032 #else
1033 if (auto_expand) {
1034 #endif
1035 char* vf_args[] = { "osd", "1", NULL };
1036 sh_video->vfilter=vf_open_filter(&opts, sh_video->vfilter,"expand",vf_args);
1039 #ifdef CONFIG_ASS
1040 if(opts.ass_enabled) {
1041 int i;
1042 int insert = 1;
1043 if (opts.vf_settings)
1044 for (i = 0; opts.vf_settings[i].name; ++i)
1045 if (strcmp(opts.vf_settings[i].name, "ass") == 0) {
1046 insert = 0;
1047 break;
1049 if (insert) {
1050 extern vf_info_t vf_info_ass;
1051 vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
1052 char* vf_arg[] = {"auto", "1", NULL};
1053 vf_instance_t* vf_ass = vf_open_plugin(&opts,libass_vfs,sh_video->vfilter,"ass",vf_arg);
1054 if (vf_ass)
1055 sh_video->vfilter=(void*)vf_ass;
1056 else
1057 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
1060 if (ass_library) {
1061 for (i = 0; i < demuxer->num_attachments; ++i) {
1062 demux_attachment_t* att = demuxer->attachments + i;
1063 if (use_embedded_fonts && attachment_is_font(att))
1064 ass_add_font(ass_library, att->name, att->data, att->data_size);
1068 #endif
1070 sh_video->vfilter=append_filters(sh_video->vfilter, opts.vf_settings);
1072 #ifdef CONFIG_ASS
1073 if (opts.ass_enabled)
1074 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
1075 #endif
1077 // after reading video params we should load subtitles because
1078 // we know fps so now we can adjust subtitles time to ~6 seconds AST
1079 // check .sub
1080 if(sub_name && sub_name[0]){
1081 for (i = 0; sub_name[i] != NULL; ++i)
1082 add_subtitles (sub_name[i], sh_video->fps, 0);
1083 } else
1084 if(sub_auto && filename) { // auto load sub file ...
1085 char **tmp = NULL;
1086 int i = 0;
1087 char *psub = get_path( "sub/" );
1088 tmp = sub_filenames((psub ? psub : ""), filename);
1089 free(psub);
1090 while (tmp[i])
1092 add_subtitles (tmp[i], sh_video->fps, 0);
1093 free(tmp[i++]);
1095 free(tmp);
1098 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1099 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
1100 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1101 if(!sh_video->initialized) mencoder_exit(1,NULL);
1105 if (!curfile) {
1106 /* force output fourcc to .. */
1107 if ((force_fourcc != NULL) && (strlen(force_fourcc) >= 4))
1109 mux_v->bih->biCompression = mmioFOURCC(force_fourcc[0], force_fourcc[1],
1110 force_fourcc[2], force_fourcc[3]);
1111 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Forcing output FourCC to %x [%.4s].\n",
1112 mux_v->bih->biCompression, (char *)&mux_v->bih->biCompression);
1115 if (! ignore_start)
1116 muxer->audio_delay_fix -= sh_video->stream_delay;
1118 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
1120 // ============= AUDIO ===============
1121 if(sh_audio){
1123 if (force_audiofmttag != -1) {
1124 sh_audio->format = force_audiofmttag;
1125 if (sh_audio->wf) {
1126 sh_audio->wf->wFormatTag = sh_audio->format;
1128 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Forcing output audio format tag to 0x%x.\n",
1129 force_audiofmttag);
1132 mux_a=muxer_new_stream(muxer,MUXER_TYPE_AUDIO);
1134 mux_a->buffer_size=0x100000; //16384;
1135 mux_a->buffer=malloc(mux_a->buffer_size);
1136 if (!mux_a->buffer)
1137 mencoder_exit(1,_("Memory allocation failed.\n"));
1139 mux_a->source=sh_audio;
1141 mux_a->codec=out_audio_codec;
1143 ao_data.samplerate = force_srate;
1144 ao_data.channels = 0;
1145 ao_data.format = audio_output_format;
1146 if(!init_audio_filters(sh_audio,
1147 // input:
1148 new_srate,
1149 // output:
1150 &ao_data.samplerate, &ao_data.channels, &ao_data.format)) {
1151 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Error at audio filter chain pre-init!\n");
1152 mencoder_exit(1, NULL);
1155 aparams.channels = ao_data.channels;
1156 aparams.sample_rate = ao_data.samplerate;
1157 aparams.audio_preload = 1000 * audio_preload;
1158 if(mux_a->codec != ACODEC_COPY) {
1159 aencoder = new_audio_encoder(mux_a, &aparams);
1160 if(!aencoder)
1161 mencoder_exit(1, NULL);
1162 if(!init_audio_filters(sh_audio,
1163 new_srate,
1164 &aparams.sample_rate, &aparams.channels, &aencoder->input_format)) {
1165 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Couldn't find matching filter/ao format!\n");
1166 mencoder_exit(1,NULL);
1170 #define NoSpeedWithFrameCopy _("WARNING: -speed is not guaranteed to work correctly with -oac copy!\n"\
1171 "Your encode might be broken!\n")
1173 switch(mux_a->codec){
1174 case ACODEC_COPY:
1175 if (opts.playback_speed != 1.0) mp_tmsg(MSGT_CPLAYER, MSGL_WARN, NoSpeedWithFrameCopy);
1176 if (sh_audio->format >= 0x10000) {
1177 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);
1178 mencoder_exit(1,NULL);
1180 if (sh_audio->wf){
1181 mux_a->wf=malloc(sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
1182 memcpy(mux_a->wf, sh_audio->wf, sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
1183 if(!sh_audio->i_bps) sh_audio->i_bps=mux_a->wf->nAvgBytesPerSec;
1184 } else {
1185 mux_a->wf = malloc(sizeof(WAVEFORMATEX));
1186 mux_a->wf->nBlockAlign = 1; //mux_a->h.dwSampleSize;
1187 mux_a->wf->wFormatTag = sh_audio->format;
1188 mux_a->wf->nChannels = sh_audio->channels;
1189 mux_a->wf->nSamplesPerSec = sh_audio->samplerate;
1190 mux_a->wf->nAvgBytesPerSec=sh_audio->i_bps; //mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
1191 mux_a->wf->wBitsPerSample = 16; // FIXME
1192 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
1194 if(sh_audio->audio.dwScale){
1195 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize;
1196 mux_a->h.dwScale=sh_audio->audio.dwScale;
1197 mux_a->h.dwRate=sh_audio->audio.dwRate;
1198 } else {
1199 mux_a->h.dwSampleSize=mux_a->wf->nBlockAlign;
1200 mux_a->h.dwScale=mux_a->h.dwSampleSize;
1201 mux_a->h.dwRate=mux_a->wf->nAvgBytesPerSec;
1203 mux_a->h.dwRate *= opts.playback_speed;
1204 mux_a->wf->nSamplesPerSec *= opts.playback_speed;
1205 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1206 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
1207 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
1208 break;
1211 if ( mp_msg_test(MSGT_MENCODER,MSGL_DBG2) ) print_wave_header(mux_a->wf, MSGL_DBG2);
1213 if (! ignore_start)
1214 muxer->audio_delay_fix += sh_audio->stream_delay;
1216 } // if(sh_audio)
1218 decoded_frameno=0;
1220 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
1221 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
1222 signal(SIGTERM,exit_sighandler); // kill
1223 signal(SIGHUP,exit_sighandler); // broken terminal line
1224 signal(SIGPIPE,exit_sighandler); // broken pipe
1226 timer_start=GetTimerMS();
1227 } // if (!curfile) // if this was the first file.
1228 else {
1229 if (!mux_a != !sh_audio) {
1230 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nCannot mix video-only files with audio and video files. Try -nosound.\n");
1231 mencoder_exit(1,NULL);
1233 if (sh_audio && mux_a->codec == ACODEC_COPY) {
1234 if (opts.playback_speed != 1.0) mp_tmsg(MSGT_CPLAYER, MSGL_WARN, NoSpeedWithFrameCopy);
1235 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1236 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
1237 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
1238 if (sh_audio->wf) {
1239 if ((mux_a->wf->wFormatTag != sh_audio->wf->wFormatTag) ||
1240 (mux_a->wf->nChannels != sh_audio->wf->nChannels) ||
1241 (mux_a->wf->nSamplesPerSec != sh_audio->wf->nSamplesPerSec * opts.playback_speed))
1243 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1244 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * opts.playback_speed),
1245 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1246 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll files must have identical audio codec and format for -oac copy.\n");
1247 mencoder_exit(1,NULL);
1249 } else {
1250 if ((mux_a->wf->wFormatTag != sh_audio->format) ||
1251 (mux_a->wf->nChannels != sh_audio->channels) ||
1252 (mux_a->wf->nSamplesPerSec != sh_audio->samplerate * opts.playback_speed))
1254 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1255 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * opts.playback_speed),
1256 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1257 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll files must have identical audio codec and format for -oac copy.\n");
1258 mencoder_exit(1,NULL);
1261 } else if (sh_audio) {
1262 int out_srate = mux_a->wf->nSamplesPerSec;
1263 int out_channels = mux_a->wf->nChannels;
1264 int out_format = aencoder->input_format;
1265 if (!init_audio_filters(sh_audio, new_srate,
1266 &out_srate, &out_channels,
1267 &out_format)) {
1268 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Couldn't find matching filter/ao format!\n");
1269 mencoder_exit(1, NULL);
1271 mux_a->wf->nSamplesPerSec = out_srate;
1272 mux_a->wf->nChannels = out_channels;
1276 if (seek_to_sec) {
1277 demux_seek(demuxer, seek_to_sec, audio_delay, 1);
1278 // there is 2 way to handle the -ss option in 3-pass mode:
1279 // > 1. do the first pass for the whole file, and use -ss for 2nd/3rd pases only
1280 // > 2. do all the 3 passes with the same -ss value
1281 // this line enables behaviour 1. (and kills 2. at the same time):
1282 // if(demuxer2) demux_seek(demuxer2, d, audio_delay, 1);
1285 if (out_file_format == MUXER_TYPE_MPEG)
1287 if (audio_preload > 0.4) {
1288 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Limiting audio preload to 0.4s.\n");
1289 audio_preload = 0.4;
1291 if (audio_density < 4) {
1292 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Increasing audio density to 4.\n");
1293 audio_density = 4;
1297 if(file_format == DEMUXER_TYPE_TV)
1299 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Forcing audio preload to 0, max pts correction to 0.\n");
1300 audio_preload = 0.0;
1301 default_max_pts_correction = 0;
1304 play_n_frames=play_n_frames_mf;
1305 if (curfile && end_at.type == END_AT_TIME) end_at.pos += mux_v->timer;
1307 if (edl_records) free_edl(edl_records);
1308 next_edl_record = edl_records = NULL;
1309 edl_muted = 0;
1310 edl_seeking = 1;
1311 if (edl_filename) {
1312 next_edl_record = edl_records = edl_parse_file();
1315 if (sh_audio && audio_delay != 0.) fixdelay(d_video, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1317 while(!at_eof){
1319 int blit_frame=0;
1320 float a_pts=0;
1321 float v_pts=0;
1322 int skip_flag=0; // 1=skip -1=duplicate
1324 if((end_at.type == END_AT_SIZE && end_at.pos <= stream_tell(muxer->stream)) ||
1325 (end_at.type == END_AT_TIME && end_at.pos < mux_v->timer))
1326 break;
1328 if(play_n_frames>=0){
1329 --play_n_frames;
1330 if(play_n_frames<0) break;
1332 if(opts.chapterrange[1]>0) {
1333 int cur_chapter = demuxer_get_current_chapter(demuxer);
1334 if(cur_chapter!=-1 && cur_chapter+1>opts.chapterrange[1])
1335 break;
1338 goto_redo_edl:
1339 if (next_edl_record && sh_video && sh_video->pts >= next_edl_record->start_sec) {
1340 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1341 float last_pos = d_video->pts;
1342 int result;
1343 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop [%f], length [%f]\n",
1344 next_edl_record->start_sec, next_edl_record->stop_sec, next_edl_record->length_sec);
1346 result = edl_seek(next_edl_record, demuxer, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1348 if (result == 2) { at_eof=1; break; } // EOF
1349 else if (result == 0) edl_seeking = 0; // no seeking
1350 else { // sucess
1351 edl_muted = 0;
1352 if (last_pos >= sh_video->pts) {
1353 // backwards seek detected!! Forget about this EDL skip altogether.
1354 next_edl_record = next_edl_record->next;
1356 else for (next_edl_record = edl_records; next_edl_record; next_edl_record = next_edl_record->next) {
1357 /* note the use of stop_sec,
1358 meaning if by some magical way we landed in the MIDDLE of a censored area,
1359 in the next loop it will jump out of it.
1361 if (next_edl_record->stop_sec > sh_video->pts) break; // we got to the right place.
1362 if (next_edl_record->action == EDL_MUTE) edl_muted = !edl_muted; // toggle mute each time.
1365 /* for a pedantic EDL, that doesn't show even a single
1366 frame from the "censored" area, uncomment next line. */
1367 goto goto_redo_edl;
1369 } else if (next_edl_record->action == EDL_MUTE) {
1370 edl_muted = !edl_muted; // This variable does nothing for now.
1371 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n", next_edl_record->start_sec );
1372 next_edl_record=next_edl_record->next;
1377 if(sh_audio){
1378 // get audio:
1379 while(mux_a->timer-audio_preload<mux_v->timer){
1380 float tottime;
1381 int len=0;
1383 ptimer_start = GetTimerMS();
1384 // CBR - copy 0.5 sec of audio
1385 // or until the end of video:
1386 tottime = stop_time(demuxer, mux_v);
1387 if (tottime != -1) {
1388 tottime -= mux_a->timer;
1389 if (tottime > 1./audio_density) tottime = 1./audio_density;
1391 else tottime = 1./audio_density;
1393 // let's not output more audio than necessary
1394 if (tottime <= 0) break;
1396 if(aencoder)
1398 if(mux_a->h.dwSampleSize) /* CBR */
1400 if(aencoder->set_decoded_len)
1402 len = mux_a->h.dwSampleSize*(int)(mux_a->h.dwRate*tottime);
1403 aencoder->set_decoded_len(aencoder, len);
1405 else
1406 len = aencoder->decode_buffer_size;
1408 len = dec_audio(sh_audio, aencoder->decode_buffer, len);
1409 mux_a->buffer_len += aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len,
1410 aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1411 if(mux_a->buffer_len < mux_a->wf->nBlockAlign)
1412 len = 0;
1413 else
1414 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1416 else /* VBR */
1418 int sz = 0;
1419 while(1)
1421 len = 0;
1422 if(! sz)
1423 sz = aencoder->get_frame_size(aencoder);
1424 if(sz > 0 && mux_a->buffer_len >= sz)
1426 len = sz;
1427 break;
1429 len = dec_audio(sh_audio,aencoder->decode_buffer, aencoder->decode_buffer_size);
1430 if(len <= 0)
1432 len = 0;
1433 break;
1435 len = aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len, aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1436 mux_a->buffer_len += len;
1439 if (mux_v->timer == 0) mux_a->h.dwInitialFrames++;
1441 else {
1442 if(mux_a->h.dwSampleSize){
1443 switch(mux_a->codec){
1444 case ACODEC_COPY: // copy
1445 len=mux_a->wf->nAvgBytesPerSec*tottime;
1446 len/=mux_a->h.dwSampleSize;if(len<1) len=1;
1447 len*=mux_a->h.dwSampleSize;
1448 len=demux_read_data(sh_audio->ds,mux_a->buffer,len);
1449 break;
1451 } else {
1452 // VBR - encode/copy an audio frame
1453 switch(mux_a->codec){
1454 case ACODEC_COPY: // copy
1455 len=ds_get_packet(sh_audio->ds,(unsigned char**) &mux_a->buffer);
1456 break;
1460 if(len<=0) break; // EOF?
1461 muxer_write_chunk(mux_a,len,AVIIF_KEYFRAME, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1462 if(!mux_a->h.dwSampleSize && mux_a->timer>0)
1463 mux_a->wf->nAvgBytesPerSec=0.5f+(double)mux_a->size/mux_a->timer; // avg bps (VBR)
1464 if(mux_a->buffer_len>=len){
1465 mux_a->buffer_len-=len;
1466 fast_memcpy(mux_a->buffer,mux_a->buffer+len,mux_a->buffer_len);
1470 audiosamples++;
1471 audiorate+= (GetTimerMS() - ptimer_start);
1476 // get video frame!
1478 if (!frame_data.already_read) {
1479 frame_data.in_size=video_read_frame(sh_video,&frame_data.frame_time,&frame_data.start,force_fps);
1480 sh_video->timer+=frame_data.frame_time;
1482 frame_data.frame_time /= opts.playback_speed;
1483 if(frame_data.in_size<0){ at_eof=1; break; }
1484 ++decoded_frameno;
1486 v_timer_corr-=frame_data.frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate;
1488 if(demuxer2){ // 3-pass encoding, read control file (frameno.avi)
1489 // find our frame:
1490 while(next_frameno<decoded_frameno){
1491 int* start;
1492 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1493 if(len<0){ at_eof=1;break;}
1494 if(len==0) --skip_flag; else // duplicate
1495 if(len==4) next_frameno=start[0];
1497 if(at_eof) break;
1498 skip_flag=next_frameno-decoded_frameno;
1499 // find next frame:
1500 while(next_frameno<=decoded_frameno){
1501 int* start;
1502 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1503 if(len<0){ at_eof=1;break;}
1504 if(len==0) --skip_flag; else // duplicate
1505 if(len==4) next_frameno=start[0];
1507 } else {
1509 // check frame duplicate/drop:
1511 float mux_frametime = (float)mux_v->h.dwScale/mux_v->h.dwRate;
1513 if (v_timer_corr >= mux_frametime && (skip_limit<0 || skip_flag < skip_limit)) {
1514 v_timer_corr-=mux_frametime;
1515 ++skip_flag; // skip
1517 while (v_timer_corr <= -mux_frametime && (skip_limit<0 || -skip_flag < skip_limit)) {
1518 v_timer_corr+=mux_frametime;
1519 --skip_flag; // dup
1522 // either v_pts_corr is big, more than 2 times framerate, then we follow its advice,
1523 // or, it cancels out v_timer_corr, in which case be happy and do nothing.
1525 while ((v_pts_corr <= -mux_frametime && skip_flag > 0) || (v_pts_corr <= -2*mux_frametime)) {
1526 v_pts_corr+=mux_frametime;
1527 --skip_flag; // dup
1529 if ((v_pts_corr >= mux_frametime && skip_flag < 0) || (v_pts_corr >= 2*mux_frametime)) {
1530 if (skip_flag<=0) { // we can't skip more than 1 frame now
1531 v_pts_corr-=mux_frametime;
1532 ++skip_flag; // skip
1536 } // demuxer2
1538 ptimer_start = GetTimerMS();
1540 switch(mux_v->codec){
1541 case VCODEC_COPY:
1542 mux_v->buffer=frame_data.start;
1543 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);
1544 break;
1545 case VCODEC_FRAMENO:
1546 mux_v->buffer=(unsigned char *)&decoded_frameno; // tricky
1547 if(skip_flag<=0) muxer_write_chunk(mux_v,sizeof(int),AVIIF_KEYFRAME, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1548 break;
1549 default:
1550 // decode_video will callback down to ve_*.c encoders, through the video filters
1551 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ, osd);
1552 {void *decoded_frame = decode_video(sh_video,frame_data.start,frame_data.in_size,
1553 skip_flag>0 && (!sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) != CONTROL_TRUE), MP_NOPTS_VALUE);
1554 blit_frame = decoded_frame && filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE);
1555 if (blit_frame) {
1556 struct vf_instance *vf = sh_video->vfilter;
1557 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
1558 vf->control(vf, VFCTRL_DRAW_OSD, osd);
1562 if (sh_video->vf_initialized < 0) mencoder_exit(1, NULL);
1564 if(!blit_frame){
1565 if (play_n_frames >= 0)
1566 play_n_frames++;
1567 badframes++;
1568 if(skip_flag<=0){
1569 // unwanted skipping of a frame, what to do?
1570 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1571 #if 0
1572 // Old code apparently made under the assumption that !blit_frame means
1573 // decoding failed due to corruption or something.. but duplication and
1574 // skipping of frames should be entirely disabled when skip_limit==0,
1575 // and must be in order for many filters to work with -noskip.
1576 // Eventually this entire block should probably be removed.
1577 if(skip_limit==0){
1578 // skipping not allowed -> write empty frame:
1579 if (!encode_duplicates || !sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1580 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1581 } else {
1582 // skipping allowed -> skip it and distriubute timer error:
1583 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1585 #endif
1590 videosamples++;
1591 videorate+=(GetTimerMS() - ptimer_start);
1593 if(skip_flag<0){
1594 // duplicate frame
1595 if(!quiet) mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\n%d duplicate frame(s)!\n",-skip_flag);
1596 while(skip_flag<0){
1597 duplicatedframes++;
1598 if (!encode_duplicates || !sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1599 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1600 ++skip_flag;
1602 } else
1603 if(skip_flag>0){
1604 // skip frame
1605 if(!quiet) mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\nSkipping frame!\n");
1606 skippedframes++;
1607 --skip_flag;
1610 if(sh_audio && !demuxer2){
1611 float AV_delay,x;
1612 // A-V sync!
1613 #if 0
1614 if(pts_from_bps){
1615 unsigned int samples=(sh_audio->audio.dwSampleSize)?
1616 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
1617 (d_audio->block_no); // <- used for VBR audio
1618 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
1619 delay_corrected=1;
1620 } else
1621 #endif
1623 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
1624 a_pts=d_audio->pts;
1625 if(!delay_corrected) if(a_pts) delay_corrected=1;
1626 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1628 v_pts=sh_video ? sh_video->pts : d_video->pts;
1629 // av = compensated (with out buffering delay) A-V diff
1630 AV_delay=(a_pts-v_pts);
1631 AV_delay-=audio_delay;
1632 AV_delay /= opts.playback_speed;
1633 AV_delay-=mux_a->timer-(mux_v->timer-(v_timer_corr+v_pts_corr));
1634 // adjust for encoder delays
1635 AV_delay -= (float) mux_a->encoder_delay * mux_a->h.dwScale/mux_a->h.dwRate;
1636 AV_delay += (float) mux_v->encoder_delay * mux_v->h.dwScale/mux_v->h.dwRate;
1637 // compensate input video timer by av:
1638 x=AV_delay*0.1f;
1639 if(x<-max_pts_correction) x=-max_pts_correction; else
1640 if(x> max_pts_correction) x= max_pts_correction;
1641 if(default_max_pts_correction>=0)
1642 max_pts_correction=default_max_pts_correction*opts.playback_speed;
1643 else
1644 max_pts_correction=sh_video->frametime*0.10 *opts.playback_speed; // +-10% of time
1645 // sh_video->timer-=x;
1646 c_total+=x;
1647 v_pts_corr+=x;
1650 { float t=(GetTimerMS()-timer_start)*0.001f;
1651 float len=(demuxer->movi_end-demuxer->movi_start);
1652 off_t pos = demuxer->filepos >= 0 ? demuxer->filepos : stream_tell(demuxer->stream);
1653 float p=len>1000 ? (float)(pos-demuxer->movi_start) / len :
1654 (demuxer_get_percent_pos(demuxer) / 100.0);
1655 #if 0
1656 if(!len && sh_audio && sh_audio->audio.dwLength>100){
1657 p=(sh_audio->audio.dwSampleSize? ds_tell(sh_audio->ds)/sh_audio->audio.dwSampleSize : sh_audio->ds->block_no)
1658 / (float)(sh_audio->audio.dwLength);
1660 #endif
1661 if(!quiet) {
1662 if( mp_msg_test(MSGT_STATUSLINE,MSGL_V) ) {
1663 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",
1664 mux_v->timer, decoded_frameno, (int)(p*100),
1665 (t>1) ? (int)(decoded_frameno/t+0.5) : 0,
1666 (p>0.001) ? (int)((t/p-t)/60) : 0,
1667 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1668 v_pts_corr,
1669 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1670 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0,
1671 audiorate/audiosamples, videorate/videosamples,
1672 duplicatedframes, badframes, skippedframes
1674 } else
1675 mp_msg(MSGT_STATUSLINE,MSGL_STATUS,"Pos:%6.1fs %6df (%2d%%) %5.2ffps Trem:%4dmin %3dmb A-V:%5.3f [%d:%d]\r",
1676 mux_v->timer, decoded_frameno, (int)(p*100),
1677 (t>1) ? (float)(decoded_frameno/t) : 0,
1678 (p>0.001) ? (int)((t/p-t)/60) : 0,
1679 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1680 v_pts_corr,
1681 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1682 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0
1686 fflush(stdout);
1688 #ifdef CONFIG_DVDREAD
1689 // DVD sub:
1690 if(vobsub_writer){
1691 unsigned char* packet=NULL;
1692 int len;
1693 while((len=ds_get_packet_sub(d_dvdsub,&packet))>0){
1694 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);
1695 vobsub_out_output(vobsub_writer,packet,len,mux_v->timer + d_dvdsub->pts - sh_video->pts);
1698 else
1699 #endif
1700 update_subtitles(NULL, &opts, sh_video, sh_video->pts, 0, d_dvdsub, 0);
1702 frame_data = (s_frame_data){ .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
1704 #if 0
1705 if(ferror(muxer_f)) {
1706 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n", out_filename);
1707 mencoder_exit(1, NULL);
1709 #endif
1711 } // while(!at_eof)
1713 if (!interrupted && filelist[++curfile].name != 0) {
1714 if (sh_video && sh_video->vfilter) { // Before uniniting sh_video and the filter chain, break apart the VE.
1715 vf_instance_t * ve; // this will be the filter right before the ve.
1716 for (ve = sh_video->vfilter; ve->next && ve->next->next; ve = ve->next);
1718 if (ve->next) ve->next = NULL; // I'm telling the last filter, before the VE, there is nothing after it
1719 else sh_video->vfilter = NULL; // There is no chain except the VE.
1722 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1723 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1724 if(demuxer) free_demuxer(demuxer);
1725 if(stream) free_stream(stream); // kill cache thread
1727 at_eof = 0;
1729 m_config_pop(mconfig);
1730 goto play_next_file;
1733 /* Emit the remaining frames in the video system */
1734 /*TODO emit frmaes delayed by decoder lag*/
1735 if(sh_video && sh_video->vfilter){
1736 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nFlushing video frames.\n");
1737 if (!sh_video->vfilter->fmt.have_configured)
1738 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Filters have not been configured! Empty file?\n");
1739 else
1740 sh_video->vfilter->control(sh_video->vfilter,
1741 VFCTRL_FLUSH_FRAMES, 0);
1744 if(aencoder)
1745 if(aencoder->fixup)
1746 aencoder->fixup(aencoder);
1748 if (muxer->cont_write_index) muxer_write_index(muxer);
1749 muxer_f_size=stream_tell(muxer->stream);
1750 stream_seek(muxer->stream,0);
1751 if (muxer->cont_write_header) muxer_write_header(muxer); // update header
1752 #if 0
1753 if(ferror(muxer_f) || fclose(muxer_f) != 0) {
1754 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n", out_filename);
1755 mencoder_exit(1, NULL);
1757 #endif
1758 if(vobsub_writer)
1759 vobsub_out_close(vobsub_writer);
1761 if(out_video_codec==VCODEC_FRAMENO && mux_v->timer>100){
1762 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));
1763 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));
1764 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));
1765 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));
1766 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));
1767 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));
1770 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nVideo stream: %8.3f kbit/s (%d B/s) size: %"PRIu64" bytes %5.3f secs %d frames\n",
1771 (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);
1772 if(sh_audio)
1773 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nAudio stream: %8.3f kbit/s (%d B/s) size: %"PRIu64" bytes %5.3f secs\n",
1774 (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);
1776 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1777 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1778 if(demuxer) free_demuxer(demuxer);
1779 if(stream) free_stream(stream); // kill cache thread
1781 return interrupted;