demux_ts: fix crash on broken packets
[mplayer/glamo.git] / mencoder.c
blob2c4f20331713adafe975dddfd7abcb02616132c4
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);
608 m_config_register_options(mconfig, common_opts);
610 // Preparse the command line
611 m_config_preparse_command_line(mconfig,argc,argv);
613 print_version("MEncoder");
615 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
616 set_path_env();
617 #endif
619 InitTimer();
621 // check codec.conf
622 if(!codecs_file || !parse_codec_cfg(codecs_file)){
623 if(!parse_codec_cfg(get_path("codecs.conf"))){
624 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
625 if(!parse_codec_cfg(NULL)){
626 mencoder_exit(1,NULL);
628 mp_tmsg(MSGT_MENCODER,MSGL_V,"Using built-in default codecs.conf.\n");
633 parse_cfgfiles(mconfig);
634 filelist = m_config_parse_me_command_line(mconfig, argc, argv);
635 if(!filelist) mencoder_exit(1, _("error parsing command line"));
638 char *extension;
640 if (!out_filename) mencoder_exit(1,_("No output file specified, please see the -o option."));
641 extension=strrchr(out_filename,'.');
642 if (extension != NULL && strlen(extension) > 3 && strlen(extension) < 6)
644 extension++;
646 switch (out_file_format)
648 case MUXER_TYPE_AVI:
649 if (strcasecmp(extension,"avi"))
650 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\nWARNING: OUTPUT FILE FORMAT IS _AVI_. See -of help.\n");
651 break;
653 case MUXER_TYPE_MPEG:
654 if (strcasecmp(extension,"mpg") &&
655 strcasecmp(extension,"mpeg") &&
656 strcasecmp(extension,"vob"))
657 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\nWARNING: OUTPUT FILE FORMAT IS _MPEG_. See -of help.\n");
658 break;
662 /* Display what configure line was used */
663 mp_msg(MSGT_MENCODER, MSGL_V, "Configuration: " CONFIGURATION "\n");
665 #define FormatNotRecognized _("============ Sorry, this file format is not recognized/supported =============\n"\
666 "=== If this file is an AVI, ASF or MPEG stream, please contact the author! ===\n")
669 if (frameno_filename) {
670 stream2=open_stream(frameno_filename,0,&i);
671 if(stream2){
672 demuxer2=demux_open(&opts, stream2,DEMUXER_TYPE_AVI,-1,-1,-2,NULL);
673 if(demuxer2) mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Using pass3 control file: %s\n", frameno_filename);
674 else mp_tmsg(MSGT_DEMUXER,MSGL_ERR, FormatNotRecognized);
678 #ifdef CONFIG_PRIORITY
679 set_priority();
680 #endif
682 if (codec_path)
683 set_codec_path(codec_path);
685 // check font
686 #ifdef CONFIG_FREETYPE
687 init_freetype();
688 #endif
689 #ifdef CONFIG_FONTCONFIG
690 if(font_fontconfig <= 0)
692 #endif
693 #ifdef CONFIG_BITMAP_FONT
694 if(font_name){
695 vo_font=read_font_desc(font_name,font_factor,verbose>1);
696 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",font_name);
697 } else {
698 // try default:
699 vo_font=read_font_desc(get_path("font/font.desc"),font_factor,verbose>1);
700 if(!vo_font)
701 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
703 #endif
704 #ifdef CONFIG_FONTCONFIG
706 #endif
708 osd = osd_create();
710 /* HACK, for some weird reason, push() has to be called twice,
711 otherwise options are not saved correctly */
712 m_config_push(mconfig);
713 play_next_file:
714 m_config_push(mconfig);
715 m_entry_set_options(mconfig,&filelist[curfile]);
716 filename = filelist[curfile].name;
718 #ifdef CONFIG_ASS
719 ass_library = ass_init();
720 #endif
722 if(!filename){
723 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "\nFilename missing.\n\n");
724 mencoder_exit(1,NULL);
726 stream=open_stream(filename,0,&file_format);
728 if(!stream){
729 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Cannot open file/device.\n");
730 mencoder_exit(1,NULL);
733 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));
735 #ifdef CONFIG_DVDREAD
736 if(stream->type==STREAMTYPE_DVD){
737 if(opts.audio_lang && opts.audio_id==-1) opts.audio_id=dvd_aid_from_lang(stream,opts.audio_lang);
738 if(opts.sub_lang && opts.sub_id==-1) opts.sub_id=dvd_sid_from_lang(stream,opts.sub_lang);
740 #endif
742 #ifdef CONFIG_DVDNAV
743 if(stream->type==STREAMTYPE_DVDNAV){
744 if(opts.audio_lang && opts.audio_id==-1) opts.audio_id=mp_dvdnav_aid_from_lang(stream,opts.audio_lang);
745 if(opts.sub_lang && opts.sub_id==-1) opts.sub_id=mp_dvdnav_sid_from_lang(stream,opts.sub_lang);
747 #endif
749 stream->start_pos+=seek_to_byte;
751 if(stream_cache_size>0) stream_enable_cache(stream,stream_cache_size*1024,0,0);
753 if(demuxer2) opts.audio_id=-2; /* do NOT read audio packets... */
755 demuxer=demux_open(&opts, stream,file_format,opts.audio_id,opts.video_id,opts.sub_id,filename);
756 if(!demuxer){
757 mp_tmsg(MSGT_DEMUXER, MSGL_FATAL, FormatNotRecognized);
758 mp_tmsg(MSGT_DEMUXER, MSGL_FATAL, "Cannot open demuxer.\n");
759 mencoder_exit(1,NULL);
762 if (ts_prog) {
763 demux_program_t prog = { .progid = ts_prog };
764 if (demux_control(demuxer, DEMUXER_CTRL_IDENTIFY_PROGRAM, &prog) != DEMUXER_CTRL_NOTIMPL) {
765 opts.audio_id = prog.aid; // switching is handled by select_audio below
766 opts.video_id = prog.vid;
767 demuxer_switch_video(demuxer, opts.video_id);
770 select_audio(demuxer, opts.audio_id, opts.audio_lang);
772 if (opts.sub_id < -1 && opts.sub_lang)
773 opts.sub_id = demuxer_sub_track_by_lang(demuxer, opts.sub_lang);
775 if (opts.sub_id < -1)
776 opts.sub_id = demuxer_default_sub_track(demuxer);
778 for (i = 0; i < MAX_S_STREAMS; i++) {
779 sh_sub_t *sh = demuxer->s_streams[i];
780 if (sh && sh->sid == opts.sub_id) {
781 demuxer->sub->id = i;
782 demuxer->sub->sh = sh;
783 break;
787 if(opts.chapterrange[0]>1) {
788 double pts;
789 if (demuxer_seek_chapter(demuxer, opts.chapterrange[0]-1, &pts, NULL) >= 0 && pts > -1.0)
790 seek_to_sec = pts;
793 d_audio=demuxer2 ? demuxer2->audio : demuxer->audio;
794 d_video=demuxer->video;
795 d_dvdsub=demuxer->sub;
796 sh_audio=d_audio->sh;
797 sh_video=d_video->sh;
799 if(!sh_video)
801 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Video stream is mandatory!\n");
802 mencoder_exit(1,NULL);
805 if(!video_read_properties(sh_video)){
806 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Video: Cannot read properties.\n");
807 mencoder_exit(1,NULL);
810 mp_tmsg(MSGT_MENCODER,MSGL_INFO, "[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
811 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
812 sh_video->fps,sh_video->frametime
815 if(force_fps){
816 sh_video->fps=force_fps;
817 sh_video->frametime=1.0f/sh_video->fps;
818 mp_tmsg(MSGT_MENCODER,MSGL_INFO,"Input fps will be interpreted as %5.3f instead.\n", sh_video->fps);
821 if(sh_audio && out_audio_codec<0){
822 if(opts.audio_id==-2)
823 mp_tmsg(MSGT_MENCODER,MSGL_ERR,"This demuxer doesn't support -nosound yet.\n");
824 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nNo audio encoder (-oac) selected. Select one (see -oac help) or use -nosound.\n");
825 mencoder_exit(1,NULL);
827 if(sh_video && out_video_codec<0){
828 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nNo video encoder (-ovc) selected. Select one (see -ovc help).\n");
829 mencoder_exit(1,NULL);
832 if(sh_audio && (out_audio_codec || seek_to_sec || !sh_audio->wf || opts.playback_speed != 1.0)){
833 // Go through the codec.conf and find the best codec...
834 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
835 if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
836 sh_audio=d_audio->sh=NULL; // failed to init :(
838 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
841 if (sh_audio) {
842 new_srate = sh_audio->samplerate;
843 if (opts.playback_speed != 1.0) {
844 new_srate *= opts.playback_speed;
845 // limits are taken from libaf/af_resample.c
846 if (new_srate < 8000) new_srate = 8000;
847 if (new_srate > 192000) new_srate = 192000;
848 opts.playback_speed = (float)new_srate / (float)sh_audio->samplerate;
852 // set up video encoder:
854 if (!curfile) { // curfile is non zero when a second file is opened
855 if (vobsub_out) {
856 unsigned int palette[16], width, height;
857 unsigned char tmp[3] = { 0, 0, 0 };
858 if (spudec_ifo && vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, opts.sub_id, tmp) >= 0)
859 vobsub_writer = vobsub_out_open(vobsub_out, palette, sh_video->disp_w, sh_video->disp_h,
860 vobsub_out_id?vobsub_out_id:(char *)tmp, vobsub_out_index);
861 #ifdef CONFIG_DVDREAD
862 if (vobsub_writer == NULL) {
863 char tmp[3];
864 if (vobsub_out_id == NULL && stream->type == STREAMTYPE_DVD) {
865 int i;
866 dvd_priv_t *dvd = (dvd_priv_t*)stream->priv;
867 for (i = 0; i < dvd->nr_of_subtitles; ++i)
868 if (dvd->subtitles[i].id == opts.sub_id) {
869 tmp[0] = (dvd->subtitles[i].language >> 8) & 0xff;
870 tmp[1] = dvd->subtitles[i].language & 0xff;
871 tmp[2] = 0;
872 vobsub_out_id = tmp;
873 break;
876 vobsub_writer=vobsub_out_open(vobsub_out, stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
877 sh_video->disp_w, sh_video->disp_h, vobsub_out_id, vobsub_out_index);
879 #endif
881 else {
882 if (spudec_ifo) {
883 unsigned int palette[16], width, height;
884 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
885 vo_spudec=spudec_new_scaled(palette, sh_video->disp_w, sh_video->disp_h, NULL, 0);
887 #ifdef CONFIG_DVDREAD
888 if (vo_spudec==NULL) {
889 vo_spudec=spudec_new_scaled(stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
890 sh_video->disp_w, sh_video->disp_h, NULL, 0);
892 #endif
893 if (vo_spudec)
894 spudec_set_forced_subs_only(vo_spudec, forced_subs_only);
897 ostream = open_output_stream(out_filename, 0);
898 if(!ostream) {
899 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, "Cannot open output file '%s'.\n", out_filename);
900 mencoder_exit(1,NULL);
903 muxer=muxer_new_muxer(out_file_format,ostream);
904 if(!muxer) {
905 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, "Cannot initialize muxer.");
906 mencoder_exit(1,NULL);
908 #if 0
909 //disabled: it horrybly distorts filtered sound
910 if(out_file_format == MUXER_TYPE_MPEG) audio_preload = 0;
911 #endif
913 muxer->audio_delay_fix = audio_delay_fix;
915 // ============= VIDEO ===============
917 mux_v=muxer_new_stream(muxer,MUXER_TYPE_VIDEO);
919 mux_v->buffer_size=0x200000; // 2MB
920 mux_v->buffer=malloc(mux_v->buffer_size);
922 mux_v->source=sh_video;
924 mux_v->h.dwSampleSize=0; // VBR
925 #ifdef CONFIG_LIBAVCODEC
927 double fps = force_ofps?force_ofps:sh_video->fps*opts.playback_speed;
928 AVRational q= av_d2q(fps, fps*1001+2);
929 mux_v->h.dwScale= q.den;
930 mux_v->h.dwRate = q.num;
932 #else
933 mux_v->h.dwScale=10000;
934 mux_v->h.dwRate=mux_v->h.dwScale*(force_ofps?force_ofps:sh_video->fps*opts.playback_speed);
935 #endif
937 mux_v->codec=out_video_codec;
939 mux_v->bih=NULL;
941 sh_video->codec=NULL;
942 sh_video->vfilter=NULL; // fixme!
944 switch(mux_v->codec){
945 case VCODEC_COPY:
946 if (!curfile) {
947 if (sh_video->bih) {
948 mux_v->bih=malloc(sh_video->bih->biSize);
949 memcpy(mux_v->bih, sh_video->bih, sh_video->bih->biSize);
951 else
953 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
954 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
955 mux_v->bih->biWidth=sh_video->disp_w;
956 mux_v->bih->biHeight=sh_video->disp_h;
957 mux_v->bih->biCompression=sh_video->format;
958 mux_v->bih->biPlanes=1;
959 mux_v->bih->biBitCount=24; // FIXME!!!
960 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
963 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
964 mux_v->bih->biWidth, mux_v->bih->biHeight,
965 mux_v->bih->biBitCount, mux_v->bih->biCompression);
967 if (curfile) {
968 if (sh_video->bih) {
969 if ((mux_v->bih->biSize != sh_video->bih->biSize) ||
970 memcmp(mux_v->bih, sh_video->bih, sh_video->bih->biSize))
972 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
973 sh_video->bih->biWidth, sh_video->bih->biHeight,
974 sh_video->bih->biBitCount, sh_video->bih->biCompression);
975 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll video files must have identical fps, resolution, and codec for -ovc copy.\n");
976 mencoder_exit(1,NULL);
979 else {
980 if ((mux_v->bih->biWidth != sh_video->disp_w) ||
981 (mux_v->bih->biHeight != sh_video->disp_h) ||
982 (mux_v->bih->biCompression != sh_video->format)) {
983 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
984 sh_video->disp_w, sh_video->disp_w, 24, sh_video->format);
985 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll video files must have identical fps, resolution, and codec for -ovc copy.\n");
986 mencoder_exit(1,NULL);
990 break;
991 case VCODEC_FRAMENO:
992 if (!curfile) {
993 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
994 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
995 mux_v->bih->biWidth=sh_video->disp_w;
996 mux_v->bih->biHeight=sh_video->disp_h;
997 mux_v->bih->biPlanes=1;
998 mux_v->bih->biBitCount=24;
999 mux_v->bih->biCompression=mmioFOURCC('F','r','N','o');
1000 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
1002 break;
1003 default: {
1004 static vf_instance_t * ve = NULL;
1005 if (!ve) {
1006 switch(mux_v->codec){
1007 case VCODEC_LIBAVCODEC:
1008 sh_video->vfilter=vf_open_encoder(&opts, NULL,"lavc",(char *)mux_v); break;
1009 case VCODEC_RAW:
1010 sh_video->vfilter=vf_open_encoder(&opts, NULL,"raw",(char *)mux_v); break;
1011 case VCODEC_VFW:
1012 sh_video->vfilter=vf_open_encoder(&opts, NULL,"vfw",(char *)mux_v); break;
1013 case VCODEC_LIBDV:
1014 sh_video->vfilter=vf_open_encoder(&opts, NULL,"libdv",(char *)mux_v); break;
1015 case VCODEC_XVID:
1016 sh_video->vfilter=vf_open_encoder(&opts, NULL,"xvid",(char *)mux_v); break;
1017 case VCODEC_QTVIDEO:
1018 sh_video->vfilter=vf_open_encoder(&opts, NULL,"qtvideo",(char *)mux_v); break;
1019 case VCODEC_NUV:
1020 sh_video->vfilter=vf_open_encoder(&opts, NULL,"nuv",(char *)mux_v); break;
1021 case VCODEC_X264:
1022 sh_video->vfilter=vf_open_encoder(&opts, NULL,"x264",(char *)mux_v); break;
1024 if(!mux_v->bih || !sh_video->vfilter){
1025 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"Failed to open the encoder.\n");
1026 mencoder_exit(1,NULL);
1028 ve = sh_video->vfilter;
1029 } else sh_video->vfilter = ve;
1030 // append 'expand' filter, it fixes stride problems and renders osd:
1031 #ifdef CONFIG_ASS
1032 if (auto_expand && !opts.ass_enabled) { /* we do not want both */
1033 #else
1034 if (auto_expand) {
1035 #endif
1036 char* vf_args[] = { "osd", "1", NULL };
1037 sh_video->vfilter=vf_open_filter(&opts, sh_video->vfilter,"expand",vf_args);
1040 #ifdef CONFIG_ASS
1041 if(opts.ass_enabled) {
1042 int i;
1043 int insert = 1;
1044 if (opts.vf_settings)
1045 for (i = 0; opts.vf_settings[i].name; ++i)
1046 if (strcmp(opts.vf_settings[i].name, "ass") == 0) {
1047 insert = 0;
1048 break;
1050 if (insert) {
1051 extern vf_info_t vf_info_ass;
1052 vf_info_t* libass_vfs[] = {&vf_info_ass, NULL};
1053 char* vf_arg[] = {"auto", "1", NULL};
1054 vf_instance_t* vf_ass = vf_open_plugin(&opts,libass_vfs,sh_video->vfilter,"ass",vf_arg);
1055 if (vf_ass)
1056 sh_video->vfilter=(void*)vf_ass;
1057 else
1058 mp_msg(MSGT_CPLAYER,MSGL_ERR, "ASS: cannot add video filter\n");
1061 if (ass_library) {
1062 for (i = 0; i < demuxer->num_attachments; ++i) {
1063 demux_attachment_t* att = demuxer->attachments + i;
1064 if (use_embedded_fonts && attachment_is_font(att))
1065 ass_add_font(ass_library, att->name, att->data, att->data_size);
1069 #endif
1071 sh_video->vfilter=append_filters(sh_video->vfilter, opts.vf_settings);
1073 #ifdef CONFIG_ASS
1074 if (opts.ass_enabled)
1075 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_INIT_EOSD, ass_library);
1076 #endif
1078 // after reading video params we should load subtitles because
1079 // we know fps so now we can adjust subtitles time to ~6 seconds AST
1080 // check .sub
1081 if(sub_name && sub_name[0]){
1082 for (i = 0; sub_name[i] != NULL; ++i)
1083 add_subtitles (sub_name[i], sh_video->fps, 0);
1084 } else
1085 if(sub_auto && filename) { // auto load sub file ...
1086 char **tmp = NULL;
1087 int i = 0;
1088 char *psub = get_path( "sub/" );
1089 tmp = sub_filenames((psub ? psub : ""), filename);
1090 free(psub);
1091 while (tmp[i])
1093 add_subtitles (tmp[i], sh_video->fps, 0);
1094 free(tmp[i++]);
1096 free(tmp);
1099 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1100 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
1101 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
1102 if(!sh_video->initialized) mencoder_exit(1,NULL);
1106 if (!curfile) {
1107 /* force output fourcc to .. */
1108 if ((force_fourcc != NULL) && (strlen(force_fourcc) >= 4))
1110 mux_v->bih->biCompression = mmioFOURCC(force_fourcc[0], force_fourcc[1],
1111 force_fourcc[2], force_fourcc[3]);
1112 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Forcing output FourCC to %x [%.4s].\n",
1113 mux_v->bih->biCompression, (char *)&mux_v->bih->biCompression);
1116 if (! ignore_start)
1117 muxer->audio_delay_fix -= sh_video->stream_delay;
1119 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
1121 // ============= AUDIO ===============
1122 if(sh_audio){
1124 if (force_audiofmttag != -1) {
1125 sh_audio->format = force_audiofmttag;
1126 if (sh_audio->wf) {
1127 sh_audio->wf->wFormatTag = sh_audio->format;
1129 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Forcing output audio format tag to 0x%x.\n",
1130 force_audiofmttag);
1133 mux_a=muxer_new_stream(muxer,MUXER_TYPE_AUDIO);
1135 mux_a->buffer_size=0x100000; //16384;
1136 mux_a->buffer=malloc(mux_a->buffer_size);
1137 if (!mux_a->buffer)
1138 mencoder_exit(1,_("Memory allocation failed.\n"));
1140 mux_a->source=sh_audio;
1142 mux_a->codec=out_audio_codec;
1144 ao_data.samplerate = force_srate;
1145 ao_data.channels = 0;
1146 ao_data.format = audio_output_format;
1147 if(!init_audio_filters(sh_audio,
1148 // input:
1149 new_srate,
1150 // output:
1151 &ao_data.samplerate, &ao_data.channels, &ao_data.format)) {
1152 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Error at audio filter chain pre-init!\n");
1153 mencoder_exit(1, NULL);
1156 aparams.channels = ao_data.channels;
1157 aparams.sample_rate = ao_data.samplerate;
1158 aparams.audio_preload = 1000 * audio_preload;
1159 if(mux_a->codec != ACODEC_COPY) {
1160 aencoder = new_audio_encoder(mux_a, &aparams);
1161 if(!aencoder)
1162 mencoder_exit(1, NULL);
1163 if(!init_audio_filters(sh_audio,
1164 new_srate,
1165 &aparams.sample_rate, &aparams.channels, &aencoder->input_format)) {
1166 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Couldn't find matching filter/ao format!\n");
1167 mencoder_exit(1,NULL);
1171 #define NoSpeedWithFrameCopy _("WARNING: -speed is not guaranteed to work correctly with -oac copy!\n"\
1172 "Your encode might be broken!\n")
1174 switch(mux_a->codec){
1175 case ACODEC_COPY:
1176 if (opts.playback_speed != 1.0) mp_tmsg(MSGT_CPLAYER, MSGL_WARN, NoSpeedWithFrameCopy);
1177 if (sh_audio->format >= 0x10000) {
1178 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);
1179 mencoder_exit(1,NULL);
1181 if (sh_audio->wf){
1182 mux_a->wf=malloc(sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
1183 memcpy(mux_a->wf, sh_audio->wf, sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
1184 if(!sh_audio->i_bps) sh_audio->i_bps=mux_a->wf->nAvgBytesPerSec;
1185 } else {
1186 mux_a->wf = malloc(sizeof(WAVEFORMATEX));
1187 mux_a->wf->nBlockAlign = 1; //mux_a->h.dwSampleSize;
1188 mux_a->wf->wFormatTag = sh_audio->format;
1189 mux_a->wf->nChannels = sh_audio->channels;
1190 mux_a->wf->nSamplesPerSec = sh_audio->samplerate;
1191 mux_a->wf->nAvgBytesPerSec=sh_audio->i_bps; //mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
1192 mux_a->wf->wBitsPerSample = 16; // FIXME
1193 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
1195 if(sh_audio->audio.dwScale){
1196 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize;
1197 mux_a->h.dwScale=sh_audio->audio.dwScale;
1198 mux_a->h.dwRate=sh_audio->audio.dwRate;
1199 } else {
1200 mux_a->h.dwSampleSize=mux_a->wf->nBlockAlign;
1201 mux_a->h.dwScale=mux_a->h.dwSampleSize;
1202 mux_a->h.dwRate=mux_a->wf->nAvgBytesPerSec;
1204 mux_a->h.dwRate *= opts.playback_speed;
1205 mux_a->wf->nSamplesPerSec *= opts.playback_speed;
1206 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1207 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
1208 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
1209 break;
1212 if ( mp_msg_test(MSGT_MENCODER,MSGL_DBG2) ) print_wave_header(mux_a->wf, MSGL_DBG2);
1214 if (! ignore_start)
1215 muxer->audio_delay_fix += sh_audio->stream_delay;
1217 } // if(sh_audio)
1219 decoded_frameno=0;
1221 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
1222 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
1223 signal(SIGTERM,exit_sighandler); // kill
1224 signal(SIGHUP,exit_sighandler); // broken terminal line
1225 signal(SIGPIPE,exit_sighandler); // broken pipe
1227 timer_start=GetTimerMS();
1228 } // if (!curfile) // if this was the first file.
1229 else {
1230 if (!mux_a != !sh_audio) {
1231 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nCannot mix video-only files with audio and video files. Try -nosound.\n");
1232 mencoder_exit(1,NULL);
1234 if (sh_audio && mux_a->codec == ACODEC_COPY) {
1235 if (opts.playback_speed != 1.0) mp_tmsg(MSGT_CPLAYER, MSGL_WARN, NoSpeedWithFrameCopy);
1236 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1237 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
1238 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
1239 if (sh_audio->wf) {
1240 if ((mux_a->wf->wFormatTag != sh_audio->wf->wFormatTag) ||
1241 (mux_a->wf->nChannels != sh_audio->wf->nChannels) ||
1242 (mux_a->wf->nSamplesPerSec != sh_audio->wf->nSamplesPerSec * opts.playback_speed))
1244 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1245 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * opts.playback_speed),
1246 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1247 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll files must have identical audio codec and format for -oac copy.\n");
1248 mencoder_exit(1,NULL);
1250 } else {
1251 if ((mux_a->wf->wFormatTag != sh_audio->format) ||
1252 (mux_a->wf->nChannels != sh_audio->channels) ||
1253 (mux_a->wf->nSamplesPerSec != sh_audio->samplerate * opts.playback_speed))
1255 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1256 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * opts.playback_speed),
1257 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1258 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll files must have identical audio codec and format for -oac copy.\n");
1259 mencoder_exit(1,NULL);
1262 } else if (sh_audio) {
1263 int out_srate = mux_a->wf->nSamplesPerSec;
1264 int out_channels = mux_a->wf->nChannels;
1265 int out_format = aencoder->input_format;
1266 if (!init_audio_filters(sh_audio, new_srate,
1267 &out_srate, &out_channels,
1268 &out_format)) {
1269 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Couldn't find matching filter/ao format!\n");
1270 mencoder_exit(1, NULL);
1272 mux_a->wf->nSamplesPerSec = out_srate;
1273 mux_a->wf->nChannels = out_channels;
1277 if (seek_to_sec) {
1278 demux_seek(demuxer, seek_to_sec, audio_delay, 1);
1279 // there is 2 way to handle the -ss option in 3-pass mode:
1280 // > 1. do the first pass for the whole file, and use -ss for 2nd/3rd pases only
1281 // > 2. do all the 3 passes with the same -ss value
1282 // this line enables behaviour 1. (and kills 2. at the same time):
1283 // if(demuxer2) demux_seek(demuxer2, d, audio_delay, 1);
1286 if (out_file_format == MUXER_TYPE_MPEG)
1288 if (audio_preload > 0.4) {
1289 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Limiting audio preload to 0.4s.\n");
1290 audio_preload = 0.4;
1292 if (audio_density < 4) {
1293 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Increasing audio density to 4.\n");
1294 audio_density = 4;
1298 if(file_format == DEMUXER_TYPE_TV)
1300 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Forcing audio preload to 0, max pts correction to 0.\n");
1301 audio_preload = 0.0;
1302 default_max_pts_correction = 0;
1305 play_n_frames=play_n_frames_mf;
1306 if (curfile && end_at.type == END_AT_TIME) end_at.pos += mux_v->timer;
1308 if (edl_records) free_edl(edl_records);
1309 next_edl_record = edl_records = NULL;
1310 edl_muted = 0;
1311 edl_seeking = 1;
1312 if (edl_filename) {
1313 next_edl_record = edl_records = edl_parse_file();
1316 if (sh_audio && audio_delay != 0.) fixdelay(d_video, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1318 while(!at_eof){
1320 int blit_frame=0;
1321 float a_pts=0;
1322 float v_pts=0;
1323 int skip_flag=0; // 1=skip -1=duplicate
1325 if((end_at.type == END_AT_SIZE && end_at.pos <= stream_tell(muxer->stream)) ||
1326 (end_at.type == END_AT_TIME && end_at.pos < mux_v->timer))
1327 break;
1329 if(play_n_frames>=0){
1330 --play_n_frames;
1331 if(play_n_frames<0) break;
1333 if(opts.chapterrange[1]>0) {
1334 int cur_chapter = demuxer_get_current_chapter(demuxer);
1335 if(cur_chapter!=-1 && cur_chapter+1>opts.chapterrange[1])
1336 break;
1339 goto_redo_edl:
1340 if (next_edl_record && sh_video && sh_video->pts >= next_edl_record->start_sec) {
1341 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1342 float last_pos = d_video->pts;
1343 int result;
1344 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop [%f], length [%f]\n",
1345 next_edl_record->start_sec, next_edl_record->stop_sec, next_edl_record->length_sec);
1347 result = edl_seek(next_edl_record, demuxer, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1349 if (result == 2) { at_eof=1; break; } // EOF
1350 else if (result == 0) edl_seeking = 0; // no seeking
1351 else { // sucess
1352 edl_muted = 0;
1353 if (last_pos >= sh_video->pts) {
1354 // backwards seek detected!! Forget about this EDL skip altogether.
1355 next_edl_record = next_edl_record->next;
1357 else for (next_edl_record = edl_records; next_edl_record; next_edl_record = next_edl_record->next) {
1358 /* note the use of stop_sec,
1359 meaning if by some magical way we landed in the MIDDLE of a censored area,
1360 in the next loop it will jump out of it.
1362 if (next_edl_record->stop_sec > sh_video->pts) break; // we got to the right place.
1363 if (next_edl_record->action == EDL_MUTE) edl_muted = !edl_muted; // toggle mute each time.
1366 /* for a pedantic EDL, that doesn't show even a single
1367 frame from the "censored" area, uncomment next line. */
1368 goto goto_redo_edl;
1370 } else if (next_edl_record->action == EDL_MUTE) {
1371 edl_muted = !edl_muted; // This variable does nothing for now.
1372 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n", next_edl_record->start_sec );
1373 next_edl_record=next_edl_record->next;
1378 if(sh_audio){
1379 // get audio:
1380 while(mux_a->timer-audio_preload<mux_v->timer){
1381 float tottime;
1382 int len=0;
1384 ptimer_start = GetTimerMS();
1385 // CBR - copy 0.5 sec of audio
1386 // or until the end of video:
1387 tottime = stop_time(demuxer, mux_v);
1388 if (tottime != -1) {
1389 tottime -= mux_a->timer;
1390 if (tottime > 1./audio_density) tottime = 1./audio_density;
1392 else tottime = 1./audio_density;
1394 // let's not output more audio than necessary
1395 if (tottime <= 0) break;
1397 if(aencoder)
1399 if(mux_a->h.dwSampleSize) /* CBR */
1401 if(aencoder->set_decoded_len)
1403 len = mux_a->h.dwSampleSize*(int)(mux_a->h.dwRate*tottime);
1404 aencoder->set_decoded_len(aencoder, len);
1406 else
1407 len = aencoder->decode_buffer_size;
1409 len = dec_audio(sh_audio, aencoder->decode_buffer, len);
1410 mux_a->buffer_len += aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len,
1411 aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1412 if(mux_a->buffer_len < mux_a->wf->nBlockAlign)
1413 len = 0;
1414 else
1415 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1417 else /* VBR */
1419 int sz = 0;
1420 while(1)
1422 len = 0;
1423 if(! sz)
1424 sz = aencoder->get_frame_size(aencoder);
1425 if(sz > 0 && mux_a->buffer_len >= sz)
1427 len = sz;
1428 break;
1430 len = dec_audio(sh_audio,aencoder->decode_buffer, aencoder->decode_buffer_size);
1431 if(len <= 0)
1433 len = 0;
1434 break;
1436 len = aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len, aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1437 mux_a->buffer_len += len;
1440 if (mux_v->timer == 0) mux_a->h.dwInitialFrames++;
1442 else {
1443 if(mux_a->h.dwSampleSize){
1444 switch(mux_a->codec){
1445 case ACODEC_COPY: // copy
1446 len=mux_a->wf->nAvgBytesPerSec*tottime;
1447 len/=mux_a->h.dwSampleSize;if(len<1) len=1;
1448 len*=mux_a->h.dwSampleSize;
1449 len=demux_read_data(sh_audio->ds,mux_a->buffer,len);
1450 break;
1452 } else {
1453 // VBR - encode/copy an audio frame
1454 switch(mux_a->codec){
1455 case ACODEC_COPY: // copy
1456 len=ds_get_packet(sh_audio->ds,(unsigned char**) &mux_a->buffer);
1457 break;
1461 if(len<=0) break; // EOF?
1462 muxer_write_chunk(mux_a,len,AVIIF_KEYFRAME, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1463 if(!mux_a->h.dwSampleSize && mux_a->timer>0)
1464 mux_a->wf->nAvgBytesPerSec=0.5f+(double)mux_a->size/mux_a->timer; // avg bps (VBR)
1465 if(mux_a->buffer_len>=len){
1466 mux_a->buffer_len-=len;
1467 fast_memcpy(mux_a->buffer,mux_a->buffer+len,mux_a->buffer_len);
1471 audiosamples++;
1472 audiorate+= (GetTimerMS() - ptimer_start);
1477 // get video frame!
1479 if (!frame_data.already_read) {
1480 frame_data.in_size=video_read_frame(sh_video,&frame_data.frame_time,&frame_data.start,force_fps);
1481 sh_video->timer+=frame_data.frame_time;
1483 frame_data.frame_time /= opts.playback_speed;
1484 if(frame_data.in_size<0){ at_eof=1; break; }
1485 ++decoded_frameno;
1487 v_timer_corr-=frame_data.frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate;
1489 if(demuxer2){ // 3-pass encoding, read control file (frameno.avi)
1490 // find our frame:
1491 while(next_frameno<decoded_frameno){
1492 int* start;
1493 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1494 if(len<0){ at_eof=1;break;}
1495 if(len==0) --skip_flag; else // duplicate
1496 if(len==4) next_frameno=start[0];
1498 if(at_eof) break;
1499 skip_flag=next_frameno-decoded_frameno;
1500 // find next frame:
1501 while(next_frameno<=decoded_frameno){
1502 int* start;
1503 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1504 if(len<0){ at_eof=1;break;}
1505 if(len==0) --skip_flag; else // duplicate
1506 if(len==4) next_frameno=start[0];
1508 } else {
1510 // check frame duplicate/drop:
1512 float mux_frametime = (float)mux_v->h.dwScale/mux_v->h.dwRate;
1514 if (v_timer_corr >= mux_frametime && (skip_limit<0 || skip_flag < skip_limit)) {
1515 v_timer_corr-=mux_frametime;
1516 ++skip_flag; // skip
1518 while (v_timer_corr <= -mux_frametime && (skip_limit<0 || -skip_flag < skip_limit)) {
1519 v_timer_corr+=mux_frametime;
1520 --skip_flag; // dup
1523 // either v_pts_corr is big, more than 2 times framerate, then we follow its advice,
1524 // or, it cancels out v_timer_corr, in which case be happy and do nothing.
1526 while ((v_pts_corr <= -mux_frametime && skip_flag > 0) || (v_pts_corr <= -2*mux_frametime)) {
1527 v_pts_corr+=mux_frametime;
1528 --skip_flag; // dup
1530 if ((v_pts_corr >= mux_frametime && skip_flag < 0) || (v_pts_corr >= 2*mux_frametime)) {
1531 if (skip_flag<=0) { // we can't skip more than 1 frame now
1532 v_pts_corr-=mux_frametime;
1533 ++skip_flag; // skip
1537 } // demuxer2
1539 ptimer_start = GetTimerMS();
1541 switch(mux_v->codec){
1542 case VCODEC_COPY:
1543 mux_v->buffer=frame_data.start;
1544 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);
1545 break;
1546 case VCODEC_FRAMENO:
1547 mux_v->buffer=(unsigned char *)&decoded_frameno; // tricky
1548 if(skip_flag<=0) muxer_write_chunk(mux_v,sizeof(int),AVIIF_KEYFRAME, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1549 break;
1550 default:
1551 // decode_video will callback down to ve_*.c encoders, through the video filters
1552 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ, osd);
1553 {void *decoded_frame = decode_video(sh_video,frame_data.start,frame_data.in_size,
1554 skip_flag>0 && (!sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) != CONTROL_TRUE), MP_NOPTS_VALUE);
1555 blit_frame = decoded_frame && filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE);
1556 if (blit_frame) {
1557 struct vf_instance *vf = sh_video->vfilter;
1558 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
1559 vf->control(vf, VFCTRL_DRAW_OSD, osd);
1563 if (sh_video->vf_initialized < 0) mencoder_exit(1, NULL);
1565 if(!blit_frame){
1566 if (play_n_frames >= 0)
1567 play_n_frames++;
1568 badframes++;
1569 if(skip_flag<=0){
1570 // unwanted skipping of a frame, what to do?
1571 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1572 #if 0
1573 // Old code apparently made under the assumption that !blit_frame means
1574 // decoding failed due to corruption or something.. but duplication and
1575 // skipping of frames should be entirely disabled when skip_limit==0,
1576 // and must be in order for many filters to work with -noskip.
1577 // Eventually this entire block should probably be removed.
1578 if(skip_limit==0){
1579 // skipping not allowed -> write empty frame:
1580 if (!encode_duplicates || !sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1581 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1582 } else {
1583 // skipping allowed -> skip it and distriubute timer error:
1584 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1586 #endif
1591 videosamples++;
1592 videorate+=(GetTimerMS() - ptimer_start);
1594 if(skip_flag<0){
1595 // duplicate frame
1596 if(!quiet) mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\n%d duplicate frame(s)!\n",-skip_flag);
1597 while(skip_flag<0){
1598 duplicatedframes++;
1599 if (!encode_duplicates || !sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1600 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1601 ++skip_flag;
1603 } else
1604 if(skip_flag>0){
1605 // skip frame
1606 if(!quiet) mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\nSkipping frame!\n");
1607 skippedframes++;
1608 --skip_flag;
1611 if(sh_audio && !demuxer2){
1612 float AV_delay,x;
1613 // A-V sync!
1614 #if 0
1615 if(pts_from_bps){
1616 unsigned int samples=(sh_audio->audio.dwSampleSize)?
1617 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
1618 (d_audio->block_no); // <- used for VBR audio
1619 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
1620 delay_corrected=1;
1621 } else
1622 #endif
1624 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
1625 a_pts=d_audio->pts;
1626 if(!delay_corrected) if(a_pts) delay_corrected=1;
1627 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1629 v_pts=sh_video ? sh_video->pts : d_video->pts;
1630 // av = compensated (with out buffering delay) A-V diff
1631 AV_delay=(a_pts-v_pts);
1632 AV_delay-=audio_delay;
1633 AV_delay /= opts.playback_speed;
1634 AV_delay-=mux_a->timer-(mux_v->timer-(v_timer_corr+v_pts_corr));
1635 // adjust for encoder delays
1636 AV_delay -= (float) mux_a->encoder_delay * mux_a->h.dwScale/mux_a->h.dwRate;
1637 AV_delay += (float) mux_v->encoder_delay * mux_v->h.dwScale/mux_v->h.dwRate;
1638 // compensate input video timer by av:
1639 x=AV_delay*0.1f;
1640 if(x<-max_pts_correction) x=-max_pts_correction; else
1641 if(x> max_pts_correction) x= max_pts_correction;
1642 if(default_max_pts_correction>=0)
1643 max_pts_correction=default_max_pts_correction*opts.playback_speed;
1644 else
1645 max_pts_correction=sh_video->frametime*0.10 *opts.playback_speed; // +-10% of time
1646 // sh_video->timer-=x;
1647 c_total+=x;
1648 v_pts_corr+=x;
1651 { float t=(GetTimerMS()-timer_start)*0.001f;
1652 float len=(demuxer->movi_end-demuxer->movi_start);
1653 off_t pos = demuxer->filepos >= 0 ? demuxer->filepos : stream_tell(demuxer->stream);
1654 float p=len>1000 ? (float)(pos-demuxer->movi_start) / len :
1655 (demuxer_get_percent_pos(demuxer) / 100.0);
1656 #if 0
1657 if(!len && sh_audio && sh_audio->audio.dwLength>100){
1658 p=(sh_audio->audio.dwSampleSize? ds_tell(sh_audio->ds)/sh_audio->audio.dwSampleSize : sh_audio->ds->block_no)
1659 / (float)(sh_audio->audio.dwLength);
1661 #endif
1662 if(!quiet) {
1663 if( mp_msg_test(MSGT_STATUSLINE,MSGL_V) ) {
1664 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",
1665 mux_v->timer, decoded_frameno, (int)(p*100),
1666 (t>1) ? (int)(decoded_frameno/t+0.5) : 0,
1667 (p>0.001) ? (int)((t/p-t)/60) : 0,
1668 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1669 v_pts_corr,
1670 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1671 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0,
1672 audiorate/audiosamples, videorate/videosamples,
1673 duplicatedframes, badframes, skippedframes
1675 } else
1676 mp_msg(MSGT_STATUSLINE,MSGL_STATUS,"Pos:%6.1fs %6df (%2d%%) %5.2ffps Trem:%4dmin %3dmb A-V:%5.3f [%d:%d]\r",
1677 mux_v->timer, decoded_frameno, (int)(p*100),
1678 (t>1) ? (float)(decoded_frameno/t) : 0,
1679 (p>0.001) ? (int)((t/p-t)/60) : 0,
1680 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1681 v_pts_corr,
1682 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1683 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0
1687 fflush(stdout);
1689 #ifdef CONFIG_DVDREAD
1690 // DVD sub:
1691 if(vobsub_writer){
1692 unsigned char* packet=NULL;
1693 int len;
1694 while((len=ds_get_packet_sub(d_dvdsub,&packet))>0){
1695 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);
1696 vobsub_out_output(vobsub_writer,packet,len,mux_v->timer + d_dvdsub->pts - sh_video->pts);
1699 else
1700 #endif
1701 update_subtitles(NULL, &opts, sh_video, sh_video->pts, 0, d_dvdsub, 0);
1703 frame_data = (s_frame_data){ .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
1705 #if 0
1706 if(ferror(muxer_f)) {
1707 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n", out_filename);
1708 mencoder_exit(1, NULL);
1710 #endif
1712 } // while(!at_eof)
1714 if (!interrupted && filelist[++curfile].name != 0) {
1715 if (sh_video && sh_video->vfilter) { // Before uniniting sh_video and the filter chain, break apart the VE.
1716 vf_instance_t * ve; // this will be the filter right before the ve.
1717 for (ve = sh_video->vfilter; ve->next && ve->next->next; ve = ve->next);
1719 if (ve->next) ve->next = NULL; // I'm telling the last filter, before the VE, there is nothing after it
1720 else sh_video->vfilter = NULL; // There is no chain except the VE.
1723 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1724 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1725 if(demuxer) free_demuxer(demuxer);
1726 if(stream) free_stream(stream); // kill cache thread
1728 at_eof = 0;
1730 m_config_pop(mconfig);
1731 goto play_next_file;
1734 /* Emit the remaining frames in the video system */
1735 /*TODO emit frmaes delayed by decoder lag*/
1736 if(sh_video && sh_video->vfilter){
1737 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nFlushing video frames.\n");
1738 if (!sh_video->vfilter->fmt.have_configured)
1739 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Filters have not been configured! Empty file?\n");
1740 else
1741 sh_video->vfilter->control(sh_video->vfilter,
1742 VFCTRL_FLUSH_FRAMES, 0);
1745 if(aencoder)
1746 if(aencoder->fixup)
1747 aencoder->fixup(aencoder);
1749 if (muxer->cont_write_index) muxer_write_index(muxer);
1750 muxer_f_size=stream_tell(muxer->stream);
1751 stream_seek(muxer->stream,0);
1752 if (muxer->cont_write_header) muxer_write_header(muxer); // update header
1753 #if 0
1754 if(ferror(muxer_f) || fclose(muxer_f) != 0) {
1755 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n", out_filename);
1756 mencoder_exit(1, NULL);
1758 #endif
1759 if(vobsub_writer)
1760 vobsub_out_close(vobsub_writer);
1762 if(out_video_codec==VCODEC_FRAMENO && mux_v->timer>100){
1763 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));
1764 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));
1765 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));
1766 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));
1767 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));
1768 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));
1771 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nVideo stream: %8.3f kbit/s (%d B/s) size: %"PRIu64" bytes %5.3f secs %d frames\n",
1772 (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);
1773 if(sh_audio)
1774 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nAudio stream: %8.3f kbit/s (%d B/s) size: %"PRIu64" bytes %5.3f secs\n",
1775 (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);
1777 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1778 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1779 if(demuxer) free_demuxer(demuxer);
1780 if(stream) free_stream(stream); // kill cache thread
1782 return interrupted;