Merge remote branch 'mplayer/tmp_build'
[mplayer/glamo.git] / mencoder.c
blob7f4094f663a5ecab18054f3a011fa260b3f04b56
1 #define VCODEC_COPY 0
2 #define VCODEC_FRAMENO 1
3 // real codecs:
4 #define VCODEC_LIBAVCODEC 4
5 #define VCODEC_VFW 7
6 #define VCODEC_LIBDV 8
7 #define VCODEC_XVID 9
8 #define VCODEC_QTVIDEO 10
9 #define VCODEC_NUV 11
10 #define VCODEC_RAW 12
11 #define VCODEC_X264 13
13 #define ACODEC_COPY 0
14 #define ACODEC_PCM 1
15 #define ACODEC_VBRMP3 2
16 #define ACODEC_NULL 3
17 #define ACODEC_LAVC 4
18 #define ACODEC_TOOLAME 5
19 #define ACODEC_FAAC 6
20 #define ACODEC_TWOLAME 7
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <signal.h>
26 #include "config.h"
28 #ifdef __MINGW32__
29 #define SIGHUP 1
30 #define SIGQUIT 3
31 #define SIGPIPE 13
32 #endif
33 #if defined(__MINGW32__) || defined(__CYGWIN__)
34 #include <windows.h>
35 #endif
37 #include <sys/time.h>
39 #include "mp_msg.h"
40 #include "help_mp.h"
42 #include "codec-cfg.h"
43 #include "m_option.h"
44 #include "m_config.h"
45 #include "parser-mecmd.h"
46 #include "parser-cfg.h"
48 #include "get_path.h"
50 #include "stream/stream.h"
51 #include "libmpdemux/demuxer.h"
52 #include "libmpdemux/stheader.h"
53 #include "libmpdemux/mp3_hdr.h"
54 #include "libmpdemux/muxer.h"
57 #include "libvo/video_out.h"
59 #include "libaf/af_format.h"
61 #include "libmpcodecs/mp_image.h"
62 #include "libmpcodecs/dec_audio.h"
63 #include "libmpcodecs/dec_video.h"
64 #include "libmpcodecs/vf.h"
65 #include "libmpcodecs/vd.h"
67 // for MPEGLAYER3WAVEFORMAT:
68 #include "libmpdemux/ms_hdr.h"
70 #include <inttypes.h>
72 #include "libvo/fastmemcpy.h"
74 #include "osdep/timer.h"
76 #ifdef CONFIG_DVDREAD
77 #include "stream/stream_dvd.h"
78 #endif
80 #include "stream/stream_dvdnav.h"
81 #include "libavcodec/avcodec.h"
83 #include "libmpcodecs/ae.h"
84 #include "options.h"
85 #include "defaultopts.h"
87 MPOpts opts;
88 struct osd_state *osd;
90 const int under_mencoder = 1;
91 int vo_doublebuffering=0;
92 int vo_directrendering=0;
93 int forced_subs_only=0;
95 //--------------------------
97 // cache2:
98 int stream_cache_size=-1;
99 #ifdef CONFIG_STREAM_CACHE
100 extern int cache_fill_status;
102 float stream_cache_min_percent=20.0;
103 float stream_cache_seek_min_percent=50.0;
104 #else
105 #define cache_fill_status 0
106 #endif
108 int vobsub_id=-1;
109 char* audio_lang=NULL;
110 char* dvdsub_lang=NULL;
111 static char* spudec_ifo=NULL;
113 static char** audio_codec_list=NULL; // override audio codec
114 static char** video_codec_list=NULL; // override video codec
115 static char** audio_fm_list=NULL; // override audio codec family
116 static char** video_fm_list=NULL; // override video codec family
117 extern char *demuxer_name; // override demuxer
118 extern char *audio_demuxer_name; // override audio demuxer
119 extern char *sub_demuxer_name; // override sub demuxer
121 static int out_audio_codec=-1;
122 static int out_video_codec=-1;
124 int out_file_format=MUXER_TYPE_AVI; // default to AVI
126 // audio stream skip/resync functions requires only for seeking.
127 // (they should be implemented in the audio codec layer)
128 //void skip_audio_frame(sh_audio_t *sh_audio){}
129 //void resync_audio_stream(sh_audio_t *sh_audio){}
131 int quiet=0;
132 double video_time_usage=0;
133 double vout_time_usage=0;
134 double max_video_time_usage=0;
135 double max_vout_time_usage=0;
136 double cur_video_time_usage=0;
137 double cur_vout_time_usage=0;
138 int benchmark=0;
140 #include "osdep/priority.h"
142 // A-V sync:
143 int delay_corrected=1;
144 static float default_max_pts_correction=-1;//0.01f;
145 static float max_pts_correction=0;//default_max_pts_correction;
146 static float c_total=0;
148 static float audio_preload=0.5;
149 static float audio_delay_fix=0.0;
150 static float audio_delay=0.0;
151 static int ignore_start=0;
152 static int audio_density=2;
154 double force_fps=0;
155 static double force_ofps=0; // set to 24 for inverse telecine
156 static int skip_limit=-1;
158 static int force_srate=0;
159 static int audio_output_format=0;
161 char *vobsub_out=NULL;
162 unsigned int vobsub_out_index=0;
163 char *vobsub_out_id=NULL;
165 char* out_filename=NULL;
167 char *force_fourcc=NULL;
168 int force_audiofmttag=-1;
170 char* passtmpfile="divx2pass.log";
172 static int play_n_frames=-1;
173 static int play_n_frames_mf=-1;
175 #include "libvo/font_load.h"
176 #include "libvo/sub.h"
178 // sub:
179 char *font_name=NULL;
180 char *sub_font_name=NULL;
181 extern int font_fontconfig;
182 float font_factor=0.75;
183 char **sub_name=NULL;
184 float sub_delay=0;
185 float sub_fps=0;
186 int sub_auto = 0;
187 int subcc_enabled=0;
188 int suboverlap_enabled = 1;
190 int auto_expand=1;
191 int encode_duplicates=1;
193 // infos are empty by default
194 char *info_name=NULL;
195 char *info_artist=NULL;
196 char *info_genre=NULL;
197 char *info_subject=NULL;
198 char *info_copyright=NULL;
199 char *info_sourceform=NULL;
200 char *info_comment=NULL;
202 // Needed by libmpcodecs vf_vo.c
203 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
204 uint32_t d_width, uint32_t d_height, uint32_t flags,
205 char *title, uint32_t format) { abort(); }
206 int vo_control(struct vo *vo, uint32_t request, void *data) { abort(); }
207 int vo_draw_image(struct vo *vo, struct mp_image *mpi, double pts) { abort(); }
208 int vo_draw_frame(struct vo *vo, uint8_t *src[]) { abort(); }
209 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y) { abort(); }
210 void vo_draw_osd(struct vo *vo, struct osd_state *osd) { abort(); }
211 void vo_flip_page(struct vo *vo) { abort(); }
212 void vo_check_events(struct vo *vo) { abort(); }
214 // Needed by getch2
215 struct mp_fifo;
216 void mplayer_put_key(struct mp_fifo *fifo, int code)
220 #include "ass_mp.h"
221 char *current_module;
222 #include "mpcommon.h"
224 // Needed by mpcommon.c
225 void set_osd_subtitle(struct MPContext *mpctx, subtitle *subs) {
226 vo_sub = subs;
227 vo_osd_changed(OSDTYPE_SUBTITLE);
230 //char *out_audio_codec=NULL; // override audio codec
231 //char *out_video_codec=NULL; // override video codec
233 //#include "libmpeg2/mpeg2.h"
234 //#include "libmpeg2/mpeg2_internal.h"
236 //static int vo_w=0, vo_h=0;
238 //-------------------------- config stuff:
240 m_config_t* mconfig;
242 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
244 static int cfg_include(m_option_t *conf, char *filename){
245 return m_config_parse_config_file(mconfig, filename);
248 static double seek_to_sec;
249 static off_t seek_to_byte=0;
251 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
253 static char * frameno_filename=NULL;
255 //static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width, int height);
257 typedef struct {
258 unsigned char* start;
259 int in_size;
260 float frame_time;
261 int already_read;
262 } s_frame_data;
264 /// Returns a_pts
265 static float calc_a_pts(demux_stream_t *d_audio);
266 /** \brief Seeks audio forward to pts by dumping audio packets
267 \return The current audio pts.
269 static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a);
270 /** \brief Seeks slowly by dumping frames.
271 \return 1 for success, 2 for EOF.
273 static int slowseek(float end_pts, demux_stream_t *d_video, demux_stream_t *d_audio, muxer_stream_t* mux_a, s_frame_data * frame_data, int framecopy, int print_info);
274 /// Deletes audio or video as told by -delay to sync
275 static void fixdelay(demux_stream_t *d_video, demux_stream_t *d_audio, muxer_stream_t* mux_a, s_frame_data * frame_data, int framecopy);
277 #include "edl.h"
278 static edl_record_ptr edl_records = NULL; ///< EDL entries memory area
279 static edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
280 static short edl_muted; ///< Stores whether EDL is currently in muted mode.
281 static short edl_seeking; ///< When non-zero, stream is seekable.
282 static short edl_seek_type; ///< When non-zero, frames are discarded instead of seeking.
283 /** \brief Seeks for EDL
284 \return 1 for success, 0 for failure, 2 for EOF.
286 static int edl_seek(edl_record_ptr next_edl_record, demuxer_t* demuxer, demux_stream_t *d_audio, muxer_stream_t* mux_a, s_frame_data * frame_data, int framecopy);
288 #include "cfg-mencoder.h"
290 #include "spudec.h"
291 #include "vobsub.h"
293 #include "libao2/audio_out.h"
294 /* FIXME */
295 static void mencoder_exit(int level, const char *how)
297 if (how)
298 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nExiting... (%s)\n", how);
299 else
300 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nExiting...\n");
302 exit(level);
305 static void parse_cfgfiles( m_config_t* conf )
307 char *conffile;
308 if (!disable_system_conf &&
309 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mencoder.conf") < 0)
310 mencoder_exit(1,_("config file error"));
312 if (!disable_user_conf) {
313 if ((conffile = get_path("mencoder.conf")) == NULL) {
314 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"get_path(\"config\") problem\n");
315 } else {
316 if (m_config_parse_config_file(conf, conffile) < 0)
317 mencoder_exit(1,_("config file error"));
318 free(conffile);
324 //---------------------------------------------------------------------------
326 static int dec_audio(sh_audio_t *sh_audio,unsigned char* buffer,int total){
327 int size=0;
328 int at_eof=0;
329 while(size<total && !at_eof){
330 int len=total-size;
331 if(len>MAX_OUTBURST) len=MAX_OUTBURST;
332 if (decode_audio(sh_audio, len) < 0) at_eof=1;
333 if(len>sh_audio->a_out_buffer_len) len=sh_audio->a_out_buffer_len;
334 fast_memcpy(buffer+size,sh_audio->a_out_buffer,len);
335 sh_audio->a_out_buffer_len-=len; size+=len;
336 if(sh_audio->a_out_buffer_len>0)
337 fast_memcpy(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[len],sh_audio->a_out_buffer_len);
339 return size;
342 //---------------------------------------------------------------------------
344 // this function returns the absoloute time for which MEncoder will switch files or move in the file.
345 // so audio can be cut correctly. -1 if there is no limit.
346 static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v);
348 static volatile int at_eof=0;
349 static volatile int interrupted=0;
351 static void exit_sighandler(int x){
352 at_eof=1;
353 interrupted=2; /* 1 means error */
356 static muxer_t* muxer=NULL;
358 void print_wave_header(WAVEFORMATEX *h, int verbose_level);
360 int main(int argc,char* argv[]){
362 stream_t* stream=NULL;
363 stream_t* ostream=NULL;
364 demuxer_t* demuxer=NULL;
365 stream_t* stream2=NULL;
366 demuxer_t* demuxer2=NULL;
367 demux_stream_t *d_audio=NULL;
368 demux_stream_t *d_video=NULL;
369 demux_stream_t *d_dvdsub=NULL;
370 sh_audio_t *sh_audio=NULL;
371 sh_video_t *sh_video=NULL;
372 int file_format=DEMUXER_TYPE_UNKNOWN;
373 int i=DEMUXER_TYPE_UNKNOWN;
374 void *vobsub_writer=NULL;
375 s_frame_data frame_data = { .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
377 uint32_t ptimer_start;
378 uint32_t audiorate=0;
379 uint32_t videorate=0;
380 uint32_t audiosamples=1;
381 uint32_t videosamples=1;
382 uint32_t skippedframes=0;
383 uint32_t duplicatedframes=0;
384 uint32_t badframes=0;
386 muxer_stream_t* mux_a=NULL;
387 muxer_stream_t* mux_v=NULL;
388 off_t muxer_f_size=0;
390 double v_pts_corr=0;
391 double v_timer_corr=0;
393 m_entry_t* filelist = NULL;
394 char* filename=NULL;
396 int decoded_frameno=0;
397 int next_frameno=-1;
398 int curfile=0;
399 int new_srate=0;
401 unsigned int timer_start=0;
402 ao_data_t ao_data = {0,0,0,0,OUTBURST,-1,0};
404 audio_encoding_params_t aparams;
405 audio_encoder_t *aencoder = NULL;
407 mp_msg_init();
409 // Create the config context and register the options
410 set_default_mencoder_options(&opts);
411 mconfig = m_config_new(&opts, cfg_include);
412 m_config_register_options(mconfig,mencoder_opts);
414 // Preparse the command line
415 m_config_preparse_command_line(mconfig,argc,argv);
417 print_version("MEncoder");
419 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
420 set_path_env();
421 #endif
423 InitTimer();
425 // check codec.conf
426 if(!codecs_file || !parse_codec_cfg(codecs_file)){
427 if(!parse_codec_cfg(get_path("codecs.conf"))){
428 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
429 if(!parse_codec_cfg(NULL)){
430 mencoder_exit(1,NULL);
432 mp_tmsg(MSGT_MENCODER,MSGL_V,"Using built-in default codecs.conf.\n");
437 parse_cfgfiles(mconfig);
438 filelist = m_config_parse_me_command_line(mconfig, argc, argv);
439 if(!filelist) mencoder_exit(1, _("error parsing command line"));
442 char *extension;
444 if (!out_filename) mencoder_exit(1,_("No output file specified, please see the -o option."));
445 extension=strrchr(out_filename,'.');
446 if (extension != NULL && strlen(extension) > 3 && strlen(extension) < 6)
448 extension++;
450 switch (out_file_format)
452 case MUXER_TYPE_AVI:
453 if (strcasecmp(extension,"avi"))
454 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\nWARNING: OUTPUT FILE FORMAT IS _AVI_. See -of help.\n");
455 break;
457 case MUXER_TYPE_MPEG:
458 if (strcasecmp(extension,"mpg") &&
459 strcasecmp(extension,"mpeg") &&
460 strcasecmp(extension,"vob"))
461 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\nWARNING: OUTPUT FILE FORMAT IS _MPEG_. See -of help.\n");
462 break;
466 /* Display what configure line was used */
467 mp_msg(MSGT_MENCODER, MSGL_V, "Configuration: " CONFIGURATION "\n");
469 #define FormatNotRecognized _("============ Sorry, this file format is not recognized/supported =============\n"\
470 "=== If this file is an AVI, ASF or MPEG stream, please contact the author! ===\n")
473 if (frameno_filename) {
474 stream2=open_stream(frameno_filename,0,&i);
475 if(stream2){
476 demuxer2=demux_open(&opts, stream2,DEMUXER_TYPE_AVI,-1,-1,-2,NULL);
477 if(demuxer2) mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Using pass3 control file: %s\n", frameno_filename);
478 else mp_tmsg(MSGT_DEMUXER,MSGL_ERR, FormatNotRecognized);
482 #ifdef CONFIG_PRIORITY
483 set_priority();
484 #endif
486 // check font
487 #ifdef CONFIG_FREETYPE
488 init_freetype();
489 #endif
490 #ifdef CONFIG_FONTCONFIG
491 if(font_fontconfig <= 0)
493 #endif
494 #ifdef CONFIG_BITMAP_FONT
495 if(font_name){
496 vo_font=read_font_desc(font_name,font_factor,verbose>1);
497 if(!vo_font) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load bitmap font: %s\n",font_name);
498 } else {
499 // try default:
500 vo_font=read_font_desc(get_path("font/font.desc"),font_factor,verbose>1);
501 if(!vo_font)
502 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
504 #endif
505 #ifdef CONFIG_FONTCONFIG
507 #endif
509 osd = osd_create();
511 /* HACK, for some weird reason, push() has to be called twice,
512 otherwise options are not saved correctly */
513 m_config_push(mconfig);
514 play_next_file:
515 m_config_push(mconfig);
516 m_entry_set_options(mconfig,&filelist[curfile]);
517 filename = filelist[curfile].name;
519 if(!filename){
520 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "\nFilename missing.\n\n");
521 mencoder_exit(1,NULL);
523 stream=open_stream(filename,0,&file_format);
525 if(!stream){
526 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Cannot open file/device.\n");
527 mencoder_exit(1,NULL);
530 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));
532 #ifdef CONFIG_DVDREAD
533 if(stream->type==STREAMTYPE_DVD){
534 if(audio_lang && opts.audio_id==-1) opts.audio_id=dvd_aid_from_lang(stream,audio_lang);
535 if(dvdsub_lang && opts.sub_id==-1) opts.sub_id=dvd_sid_from_lang(stream,dvdsub_lang);
537 #endif
539 #ifdef CONFIG_DVDNAV
540 if(stream->type==STREAMTYPE_DVDNAV){
541 if(audio_lang && opts.audio_id==-1) opts.audio_id=mp_dvdnav_aid_from_lang(stream,audio_lang);
542 if(dvdsub_lang && opts.sub_id==-1) opts.sub_id=mp_dvdnav_sid_from_lang(stream,dvdsub_lang);
544 #endif
546 stream->start_pos+=seek_to_byte;
548 if(stream_cache_size>0) stream_enable_cache(stream,stream_cache_size*1024,0,0);
550 if(demuxer2) opts.audio_id=-2; /* do NOT read audio packets... */
552 //demuxer=demux_open(stream,file_format,opts.video_id,opts.audio_id,opts.sub_id);
553 demuxer=demux_open(&opts, stream,file_format,opts.audio_id,opts.video_id,opts.sub_id,filename);
554 if(!demuxer){
555 mp_tmsg(MSGT_DEMUXER, MSGL_FATAL, FormatNotRecognized);
556 mp_tmsg(MSGT_DEMUXER, MSGL_FATAL, "Cannot open demuxer.\n");
557 mencoder_exit(1,NULL);
560 select_audio(demuxer, opts.audio_id, audio_lang);
562 if (opts.sub_id < -1 && dvdsub_lang)
563 opts.sub_id = demuxer_sub_track_by_lang(demuxer, dvdsub_lang);
565 if (opts.sub_id < -1)
566 opts.sub_id = demuxer_default_sub_track(demuxer);
568 for (i = 0; i < MAX_S_STREAMS; i++) {
569 sh_sub_t *sh = demuxer->s_streams[i];
570 if (sh && sh->sid == opts.sub_id) {
571 demuxer->sub->id = i;
572 demuxer->sub->sh = sh;
573 break;
577 if(dvd_chapter>1) {
578 double pts;
579 if (demuxer_seek_chapter(demuxer, dvd_chapter-1, &pts, NULL) >= 0 && pts > -1.0)
580 seek_to_sec = pts;
583 d_audio=demuxer2 ? demuxer2->audio : demuxer->audio;
584 d_video=demuxer->video;
585 d_dvdsub=demuxer->sub;
586 sh_audio=d_audio->sh;
587 sh_video=d_video->sh;
589 if(!sh_video)
591 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Video stream is mandatory!\n");
592 mencoder_exit(1,NULL);
595 if(!video_read_properties(sh_video)){
596 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Video: Cannot read properties.\n");
597 mencoder_exit(1,NULL);
600 mp_tmsg(MSGT_MENCODER,MSGL_INFO, "[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.3f ftime:=%6.4f\n",
601 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
602 sh_video->fps,sh_video->frametime
605 if(force_fps){
606 sh_video->fps=force_fps;
607 sh_video->frametime=1.0f/sh_video->fps;
608 mp_tmsg(MSGT_MENCODER,MSGL_INFO,"Input fps will be interpreted as %5.3f instead.\n", sh_video->fps);
611 if(sh_audio && out_audio_codec<0){
612 if(opts.audio_id==-2)
613 mp_tmsg(MSGT_MENCODER,MSGL_ERR,"This demuxer doesn't support -nosound yet.\n");
614 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nNo audio encoder (-oac) selected. Select one (see -oac help) or use -nosound.\n");
615 mencoder_exit(1,NULL);
617 if(sh_video && out_video_codec<0){
618 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nNo video encoder (-ovc) selected. Select one (see -ovc help).\n");
619 mencoder_exit(1,NULL);
622 if(sh_audio && (out_audio_codec || seek_to_sec || !sh_audio->wf || opts.playback_speed != 1.0)){
623 // Go through the codec.conf and find the best codec...
624 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
625 if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
626 sh_audio=d_audio->sh=NULL; // failed to init :(
628 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
631 if (sh_audio) {
632 new_srate = sh_audio->samplerate;
633 if (opts.playback_speed != 1.0) {
634 new_srate *= opts.playback_speed;
635 // limits are taken from libaf/af_resample.c
636 if (new_srate < 8000) new_srate = 8000;
637 if (new_srate > 192000) new_srate = 192000;
638 opts.playback_speed = (float)new_srate / (float)sh_audio->samplerate;
642 // after reading video params we should load subtitles because
643 // we know fps so now we can adjust subtitles time to ~6 seconds AST
644 // check .sub
645 // current_module="read_subtitles_file";
646 if(sub_name && sub_name[0]){
647 subdata=sub_read_file(sub_name[0], sh_video->fps);
648 if(!subdata) mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",sub_name[0]);
649 } else
650 if(sub_auto && filename) { // auto load sub file ...
651 char **tmp = NULL;
652 int i = 0;
653 char *psub = get_path( "sub/" );
654 tmp = sub_filenames((psub ? psub : ""), filename);
655 free(psub);
656 subdata=sub_read_file(tmp[0], sh_video->fps);
657 while (tmp[i])
658 free(tmp[i++]);
659 free(tmp);
662 // set up video encoder:
664 if (!curfile) { // curfile is non zero when a second file is opened
665 if (vobsub_out) {
666 unsigned int palette[16], width, height;
667 unsigned char tmp[3] = { 0, 0, 0 };
668 if (spudec_ifo && vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, opts.sub_id, tmp) >= 0)
669 vobsub_writer = vobsub_out_open(vobsub_out, palette, sh_video->disp_w, sh_video->disp_h,
670 vobsub_out_id?vobsub_out_id:(char *)tmp, vobsub_out_index);
671 #ifdef CONFIG_DVDREAD
672 if (vobsub_writer == NULL) {
673 char tmp[3];
674 if (vobsub_out_id == NULL && stream->type == STREAMTYPE_DVD) {
675 int i;
676 dvd_priv_t *dvd = (dvd_priv_t*)stream->priv;
677 for (i = 0; i < dvd->nr_of_subtitles; ++i)
678 if (dvd->subtitles[i].id == opts.sub_id) {
679 tmp[0] = (dvd->subtitles[i].language >> 8) & 0xff;
680 tmp[1] = dvd->subtitles[i].language & 0xff;
681 tmp[2] = 0;
682 vobsub_out_id = tmp;
683 break;
686 vobsub_writer=vobsub_out_open(vobsub_out, stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
687 sh_video->disp_w, sh_video->disp_h, vobsub_out_id, vobsub_out_index);
689 #endif
691 else {
692 if (spudec_ifo) {
693 unsigned int palette[16], width, height;
694 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
695 vo_spudec=spudec_new_scaled(palette, sh_video->disp_w, sh_video->disp_h, NULL, 0);
697 #ifdef CONFIG_DVDREAD
698 if (vo_spudec==NULL) {
699 vo_spudec=spudec_new_scaled(stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
700 sh_video->disp_w, sh_video->disp_h, NULL, 0);
702 #endif
703 if (vo_spudec)
704 spudec_set_forced_subs_only(vo_spudec, forced_subs_only);
707 ostream = open_output_stream(out_filename, 0);
708 if(!ostream) {
709 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, "Cannot open output file '%s'.\n", out_filename);
710 mencoder_exit(1,NULL);
713 muxer=muxer_new_muxer(out_file_format,ostream);
714 if(!muxer) {
715 mp_tmsg(MSGT_MENCODER, MSGL_FATAL, "Cannot initialize muxer.");
716 mencoder_exit(1,NULL);
718 #if 0
719 //disabled: it horrybly distorts filtered sound
720 if(out_file_format == MUXER_TYPE_MPEG) audio_preload = 0;
721 #endif
723 muxer->audio_delay_fix = audio_delay_fix;
725 // ============= VIDEO ===============
727 mux_v=muxer_new_stream(muxer,MUXER_TYPE_VIDEO);
729 mux_v->buffer_size=0x200000; // 2MB
730 mux_v->buffer=malloc(mux_v->buffer_size);
732 mux_v->source=sh_video;
734 mux_v->h.dwSampleSize=0; // VBR
735 #ifdef CONFIG_LIBAVCODEC
737 double fps = force_ofps?force_ofps:sh_video->fps*opts.playback_speed;
738 AVRational q= av_d2q(fps, fps*1001+2);
739 mux_v->h.dwScale= q.den;
740 mux_v->h.dwRate = q.num;
742 #else
743 mux_v->h.dwScale=10000;
744 mux_v->h.dwRate=mux_v->h.dwScale*(force_ofps?force_ofps:sh_video->fps*opts.playback_speed);
745 #endif
747 mux_v->codec=out_video_codec;
749 mux_v->bih=NULL;
751 sh_video->codec=NULL;
752 sh_video->vfilter=NULL; // fixme!
754 switch(mux_v->codec){
755 case VCODEC_COPY:
756 if (!curfile) {
757 if (sh_video->bih) {
758 mux_v->bih=malloc(sh_video->bih->biSize);
759 memcpy(mux_v->bih, sh_video->bih, sh_video->bih->biSize);
761 else
763 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
764 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
765 mux_v->bih->biWidth=sh_video->disp_w;
766 mux_v->bih->biHeight=sh_video->disp_h;
767 mux_v->bih->biCompression=sh_video->format;
768 mux_v->bih->biPlanes=1;
769 mux_v->bih->biBitCount=24; // FIXME!!!
770 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
773 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
774 mux_v->bih->biWidth, mux_v->bih->biHeight,
775 mux_v->bih->biBitCount, mux_v->bih->biCompression);
777 if (curfile) {
778 if (sh_video->bih) {
779 if ((mux_v->bih->biSize != sh_video->bih->biSize) ||
780 memcmp(mux_v->bih, sh_video->bih, sh_video->bih->biSize))
782 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
783 sh_video->bih->biWidth, sh_video->bih->biHeight,
784 sh_video->bih->biBitCount, sh_video->bih->biCompression);
785 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll video files must have identical fps, resolution, and codec for -ovc copy.\n");
786 mencoder_exit(1,NULL);
789 else {
790 if ((mux_v->bih->biWidth != sh_video->disp_w) ||
791 (mux_v->bih->biHeight != sh_video->disp_h) ||
792 (mux_v->bih->biCompression != sh_video->format)) {
793 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
794 sh_video->disp_w, sh_video->disp_w, 24, sh_video->format);
795 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll video files must have identical fps, resolution, and codec for -ovc copy.\n");
796 mencoder_exit(1,NULL);
800 break;
801 case VCODEC_FRAMENO:
802 if (!curfile) {
803 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
804 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
805 mux_v->bih->biWidth=sh_video->disp_w;
806 mux_v->bih->biHeight=sh_video->disp_h;
807 mux_v->bih->biPlanes=1;
808 mux_v->bih->biBitCount=24;
809 mux_v->bih->biCompression=mmioFOURCC('F','r','N','o');
810 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
812 break;
813 default: {
814 static vf_instance_t * ve = NULL;
815 if (!ve) {
816 switch(mux_v->codec){
817 case VCODEC_LIBAVCODEC:
818 sh_video->vfilter=vf_open_encoder(&opts, NULL,"lavc",(char *)mux_v); break;
819 case VCODEC_RAW:
820 sh_video->vfilter=vf_open_encoder(&opts, NULL,"raw",(char *)mux_v); break;
821 case VCODEC_VFW:
822 sh_video->vfilter=vf_open_encoder(&opts, NULL,"vfw",(char *)mux_v); break;
823 case VCODEC_LIBDV:
824 sh_video->vfilter=vf_open_encoder(&opts, NULL,"libdv",(char *)mux_v); break;
825 case VCODEC_XVID:
826 sh_video->vfilter=vf_open_encoder(&opts, NULL,"xvid",(char *)mux_v); break;
827 case VCODEC_QTVIDEO:
828 sh_video->vfilter=vf_open_encoder(&opts, NULL,"qtvideo",(char *)mux_v); break;
829 case VCODEC_NUV:
830 sh_video->vfilter=vf_open_encoder(&opts, NULL,"nuv",(char *)mux_v); break;
831 case VCODEC_X264:
832 sh_video->vfilter=vf_open_encoder(&opts, NULL,"x264",(char *)mux_v); break;
834 if(!mux_v->bih || !sh_video->vfilter){
835 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"Failed to open the encoder.\n");
836 mencoder_exit(1,NULL);
838 ve = sh_video->vfilter;
839 } else sh_video->vfilter = ve;
840 // append 'expand' filter, it fixes stride problems and renders osd:
841 if (auto_expand) {
842 char* vf_args[] = { "osd", "1", NULL };
843 sh_video->vfilter=vf_open_filter(&opts, sh_video->vfilter,"expand",vf_args);
845 sh_video->vfilter=append_filters(sh_video->vfilter, opts.vf_settings);
847 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
848 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
849 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
850 if(!sh_video->initialized) mencoder_exit(1,NULL);
854 if (!curfile) {
855 /* force output fourcc to .. */
856 if ((force_fourcc != NULL) && (strlen(force_fourcc) >= 4))
858 mux_v->bih->biCompression = mmioFOURCC(force_fourcc[0], force_fourcc[1],
859 force_fourcc[2], force_fourcc[3]);
860 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Forcing output FourCC to %x [%.4s].\n",
861 mux_v->bih->biCompression, (char *)&mux_v->bih->biCompression);
864 if (! ignore_start)
865 muxer->audio_delay_fix -= sh_video->stream_delay;
867 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
869 // ============= AUDIO ===============
870 if(sh_audio){
872 if (force_audiofmttag != -1) {
873 sh_audio->format = force_audiofmttag;
874 if (sh_audio->wf) {
875 sh_audio->wf->wFormatTag = sh_audio->format;
877 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "Forcing output audio format tag to 0x%x.\n",
878 force_audiofmttag);
881 mux_a=muxer_new_stream(muxer,MUXER_TYPE_AUDIO);
883 mux_a->buffer_size=0x100000; //16384;
884 mux_a->buffer=malloc(mux_a->buffer_size);
885 if (!mux_a->buffer)
886 mencoder_exit(1,_("Memory allocation failed.\n"));
888 mux_a->source=sh_audio;
890 mux_a->codec=out_audio_codec;
892 ao_data.samplerate = force_srate;
893 ao_data.channels = 0;
894 ao_data.format = audio_output_format;
895 if(!init_audio_filters(sh_audio,
896 // input:
897 new_srate,
898 // output:
899 &ao_data.samplerate, &ao_data.channels, &ao_data.format)) {
900 mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Error at audio filter chain pre-init!\n");
901 mencoder_exit(1, NULL);
904 aparams.channels = ao_data.channels;
905 aparams.sample_rate = ao_data.samplerate;
906 aparams.audio_preload = 1000 * audio_preload;
907 if(mux_a->codec != ACODEC_COPY) {
908 aencoder = new_audio_encoder(mux_a, &aparams);
909 if(!aencoder)
910 mencoder_exit(1, NULL);
911 if(!init_audio_filters(sh_audio,
912 new_srate,
913 &aparams.sample_rate, &aparams.channels, &aencoder->input_format)) {
914 mp_tmsg(MSGT_CPLAYER,MSGL_FATAL,"Couldn't find matching filter/ao format!\n");
915 mencoder_exit(1,NULL);
919 #define NoSpeedWithFrameCopy _("WARNING: -speed is not guaranteed to work correctly with -oac copy!\n"\
920 "Your encode might be broken!\n")
922 switch(mux_a->codec){
923 case ACODEC_COPY:
924 if (opts.playback_speed != 1.0) mp_tmsg(MSGT_CPLAYER, MSGL_WARN, NoSpeedWithFrameCopy);
925 if (sh_audio->format >= 0x10000) {
926 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);
927 mencoder_exit(1,NULL);
929 if (sh_audio->wf){
930 mux_a->wf=malloc(sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
931 memcpy(mux_a->wf, sh_audio->wf, sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
932 if(!sh_audio->i_bps) sh_audio->i_bps=mux_a->wf->nAvgBytesPerSec;
933 } else {
934 mux_a->wf = malloc(sizeof(WAVEFORMATEX));
935 mux_a->wf->nBlockAlign = 1; //mux_a->h.dwSampleSize;
936 mux_a->wf->wFormatTag = sh_audio->format;
937 mux_a->wf->nChannels = sh_audio->channels;
938 mux_a->wf->nSamplesPerSec = sh_audio->samplerate;
939 mux_a->wf->nAvgBytesPerSec=sh_audio->i_bps; //mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
940 mux_a->wf->wBitsPerSample = 16; // FIXME
941 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
943 if(sh_audio->audio.dwScale){
944 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize;
945 mux_a->h.dwScale=sh_audio->audio.dwScale;
946 mux_a->h.dwRate=sh_audio->audio.dwRate;
947 } else {
948 mux_a->h.dwSampleSize=mux_a->wf->nBlockAlign;
949 mux_a->h.dwScale=mux_a->h.dwSampleSize;
950 mux_a->h.dwRate=mux_a->wf->nAvgBytesPerSec;
952 mux_a->h.dwRate *= opts.playback_speed;
953 mux_a->wf->nSamplesPerSec *= opts.playback_speed;
954 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
955 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
956 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
957 break;
960 if ( mp_msg_test(MSGT_MENCODER,MSGL_DBG2) ) print_wave_header(mux_a->wf, MSGL_DBG2);
962 if (! ignore_start)
963 muxer->audio_delay_fix += sh_audio->stream_delay;
965 } // if(sh_audio)
967 decoded_frameno=0;
969 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
970 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
971 signal(SIGTERM,exit_sighandler); // kill
972 signal(SIGHUP,exit_sighandler); // broken terminal line
973 signal(SIGPIPE,exit_sighandler); // broken pipe
975 timer_start=GetTimerMS();
976 } // if (!curfile) // if this was the first file.
977 else {
978 if (!mux_a != !sh_audio) {
979 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nCannot mix video-only files with audio and video files. Try -nosound.\n");
980 mencoder_exit(1,NULL);
982 if (sh_audio && mux_a->codec == ACODEC_COPY) {
983 if (opts.playback_speed != 1.0) mp_tmsg(MSGT_CPLAYER, MSGL_WARN, NoSpeedWithFrameCopy);
984 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
985 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
986 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
987 if (sh_audio->wf) {
988 if ((mux_a->wf->wFormatTag != sh_audio->wf->wFormatTag) ||
989 (mux_a->wf->nChannels != sh_audio->wf->nChannels) ||
990 (mux_a->wf->nSamplesPerSec != sh_audio->wf->nSamplesPerSec * opts.playback_speed))
992 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
993 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * opts.playback_speed),
994 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
995 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll files must have identical audio codec and format for -oac copy.\n");
996 mencoder_exit(1,NULL);
998 } else {
999 if ((mux_a->wf->wFormatTag != sh_audio->format) ||
1000 (mux_a->wf->nChannels != sh_audio->channels) ||
1001 (mux_a->wf->nSamplesPerSec != sh_audio->samplerate * opts.playback_speed))
1003 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "audiocodec: framecopy (format=%x chans=%d rate=%d bits=%d B/s=%d sample-%d)\n",
1004 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * opts.playback_speed),
1005 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1006 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"\nAll files must have identical audio codec and format for -oac copy.\n");
1007 mencoder_exit(1,NULL);
1010 } else if (sh_audio) {
1011 int out_srate = mux_a->wf->nSamplesPerSec;
1012 int out_channels = mux_a->wf->nChannels;
1013 int out_format = aencoder->input_format;
1014 if (!init_audio_filters(sh_audio, new_srate,
1015 &out_srate, &out_channels,
1016 &out_format)) {
1017 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Couldn't find matching filter/ao format!\n");
1018 mencoder_exit(1, NULL);
1020 mux_a->wf->nSamplesPerSec = out_srate;
1021 mux_a->wf->nChannels = out_channels;
1025 if (seek_to_sec) {
1026 demux_seek(demuxer, seek_to_sec, audio_delay, 1);
1027 // there is 2 way to handle the -ss option in 3-pass mode:
1028 // > 1. do the first pass for the whole file, and use -ss for 2nd/3rd pases only
1029 // > 2. do all the 3 passes with the same -ss value
1030 // this line enables behaviour 1. (and kills 2. at the same time):
1031 // if(demuxer2) demux_seek(demuxer2, d, audio_delay, 1);
1034 if (out_file_format == MUXER_TYPE_MPEG)
1036 if (audio_preload > 0.4) {
1037 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Limiting audio preload to 0.4s.\n");
1038 audio_preload = 0.4;
1040 if (audio_density < 4) {
1041 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Increasing audio density to 4.\n");
1042 audio_density = 4;
1046 if(file_format == DEMUXER_TYPE_TV)
1048 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Forcing audio preload to 0, max pts correction to 0.\n");
1049 audio_preload = 0.0;
1050 default_max_pts_correction = 0;
1053 play_n_frames=play_n_frames_mf;
1054 if (curfile && end_at.type == END_AT_TIME) end_at.pos += mux_v->timer;
1056 if (edl_records) free_edl(edl_records);
1057 next_edl_record = edl_records = NULL;
1058 edl_muted = 0;
1059 edl_seeking = 1;
1060 if (edl_filename) {
1061 next_edl_record = edl_records = edl_parse_file();
1064 if (sh_audio && audio_delay != 0.) fixdelay(d_video, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1066 while(!at_eof){
1068 int blit_frame=0;
1069 float a_pts=0;
1070 float v_pts=0;
1071 int skip_flag=0; // 1=skip -1=duplicate
1073 if((end_at.type == END_AT_SIZE && end_at.pos <= stream_tell(muxer->stream)) ||
1074 (end_at.type == END_AT_TIME && end_at.pos < mux_v->timer))
1075 break;
1077 if(play_n_frames>=0){
1078 --play_n_frames;
1079 if(play_n_frames<0) break;
1081 if(dvd_last_chapter>0) {
1082 int cur_chapter = demuxer_get_current_chapter(demuxer);
1083 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
1084 break;
1087 goto_redo_edl:
1088 if (next_edl_record && sh_video && sh_video->pts >= next_edl_record->start_sec) {
1089 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1090 float last_pos = d_video->pts;
1091 int result;
1092 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop [%f], length [%f]\n",
1093 next_edl_record->start_sec, next_edl_record->stop_sec, next_edl_record->length_sec);
1095 result = edl_seek(next_edl_record, demuxer, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1097 if (result == 2) { at_eof=1; break; } // EOF
1098 else if (result == 0) edl_seeking = 0; // no seeking
1099 else { // sucess
1100 edl_muted = 0;
1101 if (last_pos >= sh_video->pts) {
1102 // backwards seek detected!! Forget about this EDL skip altogether.
1103 next_edl_record = next_edl_record->next;
1105 else for (next_edl_record = edl_records; next_edl_record; next_edl_record = next_edl_record->next) {
1106 /* note the use of stop_sec,
1107 meaning if by some magical way we landed in the MIDDLE of a censored area,
1108 in the next loop it will jump out of it.
1110 if (next_edl_record->stop_sec > sh_video->pts) break; // we got to the right place.
1111 if (next_edl_record->action == EDL_MUTE) edl_muted = !edl_muted; // toggle mute each time.
1114 /* for a pedantic EDL, that doesn't show even a single
1115 frame from the "censored" area, uncomment next line. */
1116 goto goto_redo_edl;
1118 } else if (next_edl_record->action == EDL_MUTE) {
1119 edl_muted = !edl_muted; // This variable does nothing for now.
1120 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n", next_edl_record->start_sec );
1121 next_edl_record=next_edl_record->next;
1126 if(sh_audio){
1127 // get audio:
1128 while(mux_a->timer-audio_preload<mux_v->timer){
1129 float tottime;
1130 int len=0;
1132 ptimer_start = GetTimerMS();
1133 // CBR - copy 0.5 sec of audio
1134 // or until the end of video:
1135 tottime = stop_time(demuxer, mux_v);
1136 if (tottime != -1) {
1137 tottime -= mux_a->timer;
1138 if (tottime > 1./audio_density) tottime = 1./audio_density;
1140 else tottime = 1./audio_density;
1142 // let's not output more audio than necessary
1143 if (tottime <= 0) break;
1145 if(aencoder)
1147 if(mux_a->h.dwSampleSize) /* CBR */
1149 if(aencoder->set_decoded_len)
1151 len = mux_a->h.dwSampleSize*(int)(mux_a->h.dwRate*tottime);
1152 aencoder->set_decoded_len(aencoder, len);
1154 else
1155 len = aencoder->decode_buffer_size;
1157 len = dec_audio(sh_audio, aencoder->decode_buffer, len);
1158 mux_a->buffer_len += aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len,
1159 aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1160 if(mux_a->buffer_len < mux_a->wf->nBlockAlign)
1161 len = 0;
1162 else
1163 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1165 else /* VBR */
1167 int sz = 0;
1168 while(1)
1170 len = 0;
1171 if(! sz)
1172 sz = aencoder->get_frame_size(aencoder);
1173 if(sz > 0 && mux_a->buffer_len >= sz)
1175 len = sz;
1176 break;
1178 len = dec_audio(sh_audio,aencoder->decode_buffer, aencoder->decode_buffer_size);
1179 if(len <= 0)
1181 len = 0;
1182 break;
1184 len = aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len, aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1185 mux_a->buffer_len += len;
1188 if (mux_v->timer == 0) mux_a->h.dwInitialFrames++;
1190 else {
1191 if(mux_a->h.dwSampleSize){
1192 switch(mux_a->codec){
1193 case ACODEC_COPY: // copy
1194 len=mux_a->wf->nAvgBytesPerSec*tottime;
1195 len/=mux_a->h.dwSampleSize;if(len<1) len=1;
1196 len*=mux_a->h.dwSampleSize;
1197 len=demux_read_data(sh_audio->ds,mux_a->buffer,len);
1198 break;
1200 } else {
1201 // VBR - encode/copy an audio frame
1202 switch(mux_a->codec){
1203 case ACODEC_COPY: // copy
1204 len=ds_get_packet(sh_audio->ds,(unsigned char**) &mux_a->buffer);
1205 break;
1209 if(len<=0) break; // EOF?
1210 muxer_write_chunk(mux_a,len,0x10, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1211 if(!mux_a->h.dwSampleSize && mux_a->timer>0)
1212 mux_a->wf->nAvgBytesPerSec=0.5f+(double)mux_a->size/mux_a->timer; // avg bps (VBR)
1213 if(mux_a->buffer_len>=len){
1214 mux_a->buffer_len-=len;
1215 fast_memcpy(mux_a->buffer,mux_a->buffer+len,mux_a->buffer_len);
1219 audiosamples++;
1220 audiorate+= (GetTimerMS() - ptimer_start);
1225 // get video frame!
1227 if (!frame_data.already_read) {
1228 frame_data.in_size=video_read_frame(sh_video,&frame_data.frame_time,&frame_data.start,force_fps);
1229 sh_video->timer+=frame_data.frame_time;
1231 frame_data.frame_time /= opts.playback_speed;
1232 if(frame_data.in_size<0){ at_eof=1; break; }
1233 ++decoded_frameno;
1235 v_timer_corr-=frame_data.frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate;
1237 if(demuxer2){ // 3-pass encoding, read control file (frameno.avi)
1238 // find our frame:
1239 while(next_frameno<decoded_frameno){
1240 int* start;
1241 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1242 if(len<0){ at_eof=1;break;}
1243 if(len==0) --skip_flag; else // duplicate
1244 if(len==4) next_frameno=start[0];
1246 if(at_eof) break;
1247 // if(skip_flag) printf("!!!!!!!!!!!!\n");
1248 skip_flag=next_frameno-decoded_frameno;
1249 // find next frame:
1250 while(next_frameno<=decoded_frameno){
1251 int* start;
1252 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1253 if(len<0){ at_eof=1;break;}
1254 if(len==0) --skip_flag; else // duplicate
1255 if(len==4) next_frameno=start[0];
1257 // if(at_eof) break;
1258 // printf("Current fno=%d requested=%d skip=%d \n",decoded_frameno,fno,skip_flag);
1259 } else {
1261 // check frame duplicate/drop:
1263 //printf("\r### %5.3f ###\n",v_timer_corr);
1264 float mux_frametime = (float)mux_v->h.dwScale/mux_v->h.dwRate;
1266 if (v_timer_corr >= mux_frametime && (skip_limit<0 || skip_flag < skip_limit)) {
1267 v_timer_corr-=mux_frametime;
1268 ++skip_flag; // skip
1270 while (v_timer_corr <= -mux_frametime && (skip_limit<0 || -skip_flag < skip_limit)) {
1271 v_timer_corr+=mux_frametime;
1272 --skip_flag; // dup
1275 // either v_pts_corr is big, more than 2 times framerate, then we follow its advice,
1276 // or, it cancels out v_timer_corr, in which case be happy and do nothing.
1278 while ((v_pts_corr <= -mux_frametime && skip_flag > 0) || (v_pts_corr <= -2*mux_frametime)) {
1279 v_pts_corr+=mux_frametime;
1280 --skip_flag; // dup
1282 if ((v_pts_corr >= mux_frametime && skip_flag < 0) || (v_pts_corr >= 2*mux_frametime)) {
1283 if (skip_flag<=0) { // we can't skip more than 1 frame now
1284 v_pts_corr-=mux_frametime;
1285 ++skip_flag; // skip
1289 } // demuxer2
1291 ptimer_start = GetTimerMS();
1293 switch(mux_v->codec){
1294 case VCODEC_COPY:
1295 mux_v->buffer=frame_data.start;
1296 if(skip_flag<=0) muxer_write_chunk(mux_v,frame_data.in_size,(sh_video->ds->flags&1)?0x10:0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1297 break;
1298 case VCODEC_FRAMENO:
1299 mux_v->buffer=(unsigned char *)&decoded_frameno; // tricky
1300 if(skip_flag<=0) muxer_write_chunk(mux_v,sizeof(int),0x10, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1301 break;
1302 default:
1303 // decode_video will callback down to ve_*.c encoders, through the video filters
1304 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ, osd);
1305 {void *decoded_frame = decode_video(sh_video,frame_data.start,frame_data.in_size,
1306 skip_flag>0 && (!sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) != CONTROL_TRUE), MP_NOPTS_VALUE);
1307 blit_frame = decoded_frame && filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE);
1308 if (blit_frame) {
1309 struct vf_instance *vf = sh_video->vfilter;
1310 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
1311 vf->control(vf, VFCTRL_DRAW_OSD, osd);
1315 if (sh_video->vf_initialized < 0) mencoder_exit(1, NULL);
1317 if(!blit_frame){
1318 if (play_n_frames >= 0)
1319 play_n_frames++;
1320 badframes++;
1321 if(skip_flag<=0){
1322 // unwanted skipping of a frame, what to do?
1323 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1324 #if 0
1325 // Old code apparently made under the assumption that !blit_frame means
1326 // decoding failed due to corruption or something.. but duplication and
1327 // skipping of frames should be entirely disabled when skip_limit==0,
1328 // and must be in order for many filters to work with -noskip.
1329 // Eventually this entire block should probably be removed.
1330 if(skip_limit==0){
1331 // skipping not allowed -> write empty frame:
1332 if (!encode_duplicates || !sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1333 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1334 } else {
1335 // skipping allowed -> skip it and distriubute timer error:
1336 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1338 #endif
1343 videosamples++;
1344 videorate+=(GetTimerMS() - ptimer_start);
1346 if(skip_flag<0){
1347 // duplicate frame
1348 if(!quiet) mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\n%d duplicate frame(s)!\n",-skip_flag);
1349 while(skip_flag<0){
1350 duplicatedframes++;
1351 if (!encode_duplicates || !sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1352 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1353 ++skip_flag;
1355 } else
1356 if(skip_flag>0){
1357 // skip frame
1358 if(!quiet) mp_tmsg(MSGT_MENCODER, MSGL_WARN, "\nSkipping frame!\n");
1359 skippedframes++;
1360 --skip_flag;
1363 if(sh_audio && !demuxer2){
1364 float AV_delay,x;
1365 // A-V sync!
1366 #if 0
1367 if(pts_from_bps){
1368 unsigned int samples=(sh_audio->audio.dwSampleSize)?
1369 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
1370 (d_audio->block_no); // <- used for VBR audio
1371 // printf("samples=%d \n",samples);
1372 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
1373 delay_corrected=1;
1374 } else
1375 #endif
1377 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
1378 a_pts=d_audio->pts;
1379 if(!delay_corrected) if(a_pts) delay_corrected=1;
1380 //printf("*** %5.3f ***\n",a_pts);
1381 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1383 v_pts=sh_video ? sh_video->pts : d_video->pts;
1384 // av = compensated (with out buffering delay) A-V diff
1385 AV_delay=(a_pts-v_pts);
1386 AV_delay-=audio_delay;
1387 AV_delay /= opts.playback_speed;
1388 AV_delay-=mux_a->timer-(mux_v->timer-(v_timer_corr+v_pts_corr));
1389 // adjust for encoder delays
1390 AV_delay -= (float) mux_a->encoder_delay * mux_a->h.dwScale/mux_a->h.dwRate;
1391 AV_delay += (float) mux_v->encoder_delay * mux_v->h.dwScale/mux_v->h.dwRate;
1392 // compensate input video timer by av:
1393 x=AV_delay*0.1f;
1394 if(x<-max_pts_correction) x=-max_pts_correction; else
1395 if(x> max_pts_correction) x= max_pts_correction;
1396 if(default_max_pts_correction>=0)
1397 max_pts_correction=default_max_pts_correction*opts.playback_speed;
1398 else
1399 max_pts_correction=sh_video->frametime*0.10 *opts.playback_speed; // +-10% of time
1400 // sh_video->timer-=x;
1401 c_total+=x;
1402 v_pts_corr+=x;
1405 // printf("A:%6.1f V:%6.1f A-V:%7.3f oAV:%7.3f diff:%7.3f ct:%7.3f vpc:%7.3f \r",
1406 // a_pts,v_pts,a_pts-v_pts,
1407 // (float)(mux_a->timer-mux_v->timer),
1408 // AV_delay, c_total, v_pts_corr );
1409 // printf("V:%6.1f \r", d_video->pts );
1411 #if 0
1412 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"A:%6.1f V:%6.1f A-V:%7.3f ct:%7.3f %3d/%3d %2d%% %2d%% %4.1f%% %d%%\r",
1413 a_pts,v_pts,a_pts-v_pts,c_total,
1414 (int)sh_video->num_frames,(int)sh_video->num_frames_decoded,
1415 (sh_video->timer>0.5)?(int)(100.0*video_time_usage/(double)sh_video->timer):0,
1416 (sh_video->timer>0.5)?(int)(100.0*vout_time_usage/(double)sh_video->timer):0,
1417 (sh_video->timer>0.5)?(100.0*audio_time_usage/(double)sh_video->timer):0
1418 ,cache_fill_status
1420 #endif
1422 { float t=(GetTimerMS()-timer_start)*0.001f;
1423 float len=(demuxer->movi_end-demuxer->movi_start);
1424 off_t pos = demuxer->filepos >= 0 ? demuxer->filepos : stream_tell(demuxer->stream);
1425 float p=len>1000 ? (float)(pos-demuxer->movi_start) / len :
1426 (demuxer_get_percent_pos(demuxer) / 100.0);
1427 #if 0
1428 if(!len && sh_audio && sh_audio->audio.dwLength>100){
1429 p=(sh_audio->audio.dwSampleSize? ds_tell(sh_audio->ds)/sh_audio->audio.dwSampleSize : sh_audio->ds->block_no)
1430 / (float)(sh_audio->audio.dwLength);
1432 #endif
1433 #if 0
1434 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"%d < %d < %d \r",
1435 (int)demuxer->movi_start,
1436 (int)demuxer->filepos,
1437 (int)demuxer->movi_end);
1438 #else
1439 if(!quiet) {
1440 if( mp_msg_test(MSGT_STATUSLINE,MSGL_V) ) {
1441 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",
1442 mux_v->timer, decoded_frameno, (int)(p*100),
1443 (t>1) ? (int)(decoded_frameno/t+0.5) : 0,
1444 (p>0.001) ? (int)((t/p-t)/60) : 0,
1445 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1446 v_pts_corr,
1447 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1448 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0,
1449 audiorate/audiosamples, videorate/videosamples,
1450 duplicatedframes, badframes, skippedframes
1452 } else
1453 mp_msg(MSGT_STATUSLINE,MSGL_STATUS,"Pos:%6.1fs %6df (%2d%%) %5.2ffps Trem:%4dmin %3dmb A-V:%5.3f [%d:%d]\r",
1454 mux_v->timer, decoded_frameno, (int)(p*100),
1455 (t>1) ? (float)(decoded_frameno/t) : 0,
1456 (p>0.001) ? (int)((t/p-t)/60) : 0,
1457 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1458 v_pts_corr,
1459 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1460 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0
1463 #endif
1465 fflush(stdout);
1467 #ifdef CONFIG_DVDREAD
1468 // DVD sub:
1469 if(vobsub_writer){
1470 unsigned char* packet=NULL;
1471 int len;
1472 while((len=ds_get_packet_sub(d_dvdsub,&packet))>0){
1473 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);
1474 vobsub_out_output(vobsub_writer,packet,len,mux_v->timer + d_dvdsub->pts - sh_video->pts);
1477 else
1478 #endif
1479 update_subtitles(NULL, &opts, sh_video, sh_video->pts, 0, d_dvdsub, 0);
1481 frame_data = (s_frame_data){ .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
1483 #if 0
1484 if(ferror(muxer_f)) {
1485 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n", out_filename);
1486 mencoder_exit(1, NULL);
1488 #endif
1490 } // while(!at_eof)
1492 if (!interrupted && filelist[++curfile].name != 0) {
1493 if (sh_video && sh_video->vfilter) { // Before uniniting sh_video and the filter chain, break apart the VE.
1494 vf_instance_t * ve; // this will be the filter right before the ve.
1495 for (ve = sh_video->vfilter; ve->next && ve->next->next; ve = ve->next);
1497 if (ve->next) ve->next = NULL; // I'm telling the last filter, before the VE, there is nothing after it
1498 else sh_video->vfilter = NULL; // There is no chain except the VE.
1501 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1502 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1503 if(demuxer) free_demuxer(demuxer);
1504 if(stream) free_stream(stream); // kill cache thread
1506 at_eof = 0;
1508 m_config_pop(mconfig);
1509 goto play_next_file;
1512 /* Emit the remaining frames in the video system */
1513 /*TODO emit frmaes delayed by decoder lag*/
1514 if(sh_video && sh_video->vfilter){
1515 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nFlushing video frames.\n");
1516 if (!sh_video->vfilter->fmt.have_configured)
1517 mp_tmsg(MSGT_MENCODER, MSGL_WARN, "Filters have not been configured! Empty file?\n");
1518 else
1519 sh_video->vfilter->control(sh_video->vfilter,
1520 VFCTRL_FLUSH_FRAMES, 0);
1523 if(aencoder)
1524 if(aencoder->fixup)
1525 aencoder->fixup(aencoder);
1527 if (muxer->cont_write_index) muxer_write_index(muxer);
1528 muxer_f_size=stream_tell(muxer->stream);
1529 stream_seek(muxer->stream,0);
1530 if (muxer->cont_write_header) muxer_write_header(muxer); // update header
1531 #if 0
1532 if(ferror(muxer_f) || fclose(muxer_f) != 0) {
1533 mp_tmsg(MSGT_MENCODER,MSGL_FATAL,"%s: Error writing file.\n", out_filename);
1534 mencoder_exit(1, NULL);
1536 #endif
1537 if(vobsub_writer)
1538 vobsub_out_close(vobsub_writer);
1540 if(out_video_codec==VCODEC_FRAMENO && mux_v->timer>100){
1541 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));
1542 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));
1543 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));
1544 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));
1545 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));
1546 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));
1549 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nVideo stream: %8.3f kbit/s (%d B/s) size: %"PRIu64" bytes %5.3f secs %d frames\n",
1550 (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);
1551 if(sh_audio)
1552 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "\nAudio stream: %8.3f kbit/s (%d B/s) size: %"PRIu64" bytes %5.3f secs\n",
1553 (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);
1555 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1556 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1557 if(demuxer) free_demuxer(demuxer);
1558 if(stream) free_stream(stream); // kill cache thread
1560 return interrupted;
1564 #if 0
1565 /* Flip the image in src and store the result in dst. src and dst may overlap.
1566 width is the size of each line in bytes. */
1567 static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width,
1568 int height)
1570 uint8_t* tmp = malloc(width);
1571 int i;
1573 for(i = 0; i < height/2; i++) {
1574 fast_memcpy(tmp, &src[i*width], width);
1575 fast_memcpy(&dst[i * width], &src[(height - i) * width], width);
1576 fast_memcpy(&dst[(height - i) * width], tmp, width);
1579 free(tmp);
1580 return dst;
1582 #endif
1585 static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v) {
1586 float timeleft = -1;
1587 if (play_n_frames >= 0) timeleft = mux_v->timer + play_n_frames * (double)(mux_v->h.dwScale) / mux_v->h.dwRate;
1588 if (end_at.type == END_AT_TIME && (timeleft > end_at.pos || timeleft == -1)) timeleft = end_at.pos;
1589 if (next_edl_record && demuxer && demuxer->video) { // everything is OK to be checked
1590 float tmp = mux_v->timer + next_edl_record->start_sec - demuxer->video->pts;
1591 if (timeleft == -1 || timeleft > tmp) {
1592 // There's less time in EDL than what we already know
1593 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1594 timeleft = tmp;
1595 } else if (next_edl_record->action == EDL_MUTE) {
1596 //timeleft = next_edl_record->start_sec - demuxer->video->pts;
1597 // For the moment (and probably forever) EDL mute doesn't work in MEncoder
1601 return timeleft;
1604 static float calc_a_pts(demux_stream_t *d_audio) {
1605 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
1606 float a_pts = 0.;
1607 if (sh_audio)
1608 a_pts = d_audio->pts + (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1609 return a_pts;
1612 static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a) {
1613 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
1614 int samplesize, avg;
1615 float a_pts = calc_a_pts(d_audio);
1617 if (!sh_audio) return a_pts;
1619 if (sh_audio->audio.dwScale) samplesize = sh_audio->audio.dwSampleSize;
1620 else samplesize = (sh_audio->wf ? sh_audio->wf->nBlockAlign : 1);
1621 avg = (sh_audio->wf ? sh_audio->wf->nAvgBytesPerSec : sh_audio->i_bps);
1623 // after a demux_seek, a_pts will be zero until you read some audio.
1624 // carefully checking if a_pts is truely correct by reading tiniest amount of data possible.
1625 if (pts > a_pts && a_pts == 0.0 && samplesize) {
1626 if (demux_read_data(sh_audio->ds,mux_a->buffer,samplesize) <= 0) return a_pts; // EOF
1627 a_pts = calc_a_pts(d_audio);
1630 while (pts > a_pts) {
1631 int len;
1632 if (samplesize) {
1633 len = avg * (pts - a_pts > 0.5 ? 0.5 : pts - a_pts);
1634 len/= samplesize; if(len<1) len=1;
1635 len*= samplesize;
1636 len = demux_read_data(sh_audio->ds,mux_a->buffer,len);
1637 } else {
1638 unsigned char * crap;
1639 len = ds_get_packet(sh_audio->ds, &crap);
1641 if (len <= 0) break; // EOF of audio.
1642 a_pts = calc_a_pts(d_audio);
1644 return a_pts;
1647 static int slowseek(float end_pts, demux_stream_t *d_video, demux_stream_t *d_audio, muxer_stream_t* mux_a, s_frame_data * frame_data, int framecopy, int print_info) {
1648 sh_video_t * sh_video = d_video->sh;
1649 vf_instance_t * vfilter = sh_video ? sh_video->vfilter : NULL;
1650 int done = 0;
1652 while (!interrupted) {
1653 float a_pts = 0.;
1655 if (!frame_data->already_read) { // when called after fixdelay, a frame is already read
1656 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
1657 if(frame_data->in_size<0) return 2;
1658 sh_video->timer += frame_data->frame_time;
1660 frame_data->already_read = 0;
1662 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
1664 if (done) {
1665 // wait for keyframe in case of -ovc copy
1666 if (!framecopy || (sh_video->ds->flags & 1)) {
1667 frame_data->already_read = 1;
1668 return 1;
1671 if (sh_video->pts >= end_pts) done = 1;
1673 if (vfilter) {
1674 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
1675 osd);
1676 int softskip = (vfilter->control(vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) == CONTROL_TRUE);
1677 void *decoded_frame = decode_video(sh_video, frame_data->start, frame_data->in_size, !softskip, MP_NOPTS_VALUE);
1678 if (decoded_frame)
1679 if (filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE)) {
1680 struct vf_instance *vf = sh_video->vfilter;
1681 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
1682 vf->control(vf, VFCTRL_DRAW_OSD, osd);
1686 if (print_info) mp_tmsg(MSGT_MENCODER, MSGL_STATUS,
1687 "EDL SKIP: Start: %.2f End: %.2f Current: V: %.2f A: %.2f \r",
1688 next_edl_record->start_sec, next_edl_record->stop_sec,
1689 sh_video->pts, a_pts);
1691 if (interrupted) return 2;
1692 return 1;
1695 static void fixdelay(demux_stream_t *d_video, demux_stream_t *d_audio, muxer_stream_t* mux_a, s_frame_data * frame_data, int framecopy) {
1696 // TODO: Find a way to encode silence instead of deleting video
1697 sh_video_t * sh_video = d_video->sh;
1698 float a_pts;
1700 // demux_seek has a weirdness that sh_video->pts is meaningless,
1701 // until a single frame is read... Same for audio actually too.
1702 // Reading one frame, and keeping it.
1703 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
1704 if(frame_data->in_size<0) return;
1705 sh_video->timer += frame_data->frame_time;
1706 frame_data->already_read = 1;
1708 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
1710 if (audio_delay > 0) return;
1711 else if (sh_video->pts - frame_data->frame_time + audio_delay >= a_pts) return;
1713 slowseek(a_pts - audio_delay, d_video, d_audio, mux_a, frame_data, framecopy, 0);
1716 static int edl_seek(edl_record_ptr next_edl_record, demuxer_t* demuxer, demux_stream_t *d_audio, muxer_stream_t* mux_a, s_frame_data * frame_data, int framecopy) {
1717 sh_video_t * sh_video = demuxer->video ? demuxer->video->sh : NULL;
1719 if (!sh_video) return 0;
1720 if (sh_video->pts >= next_edl_record->stop_sec) return 1; // nothing to do...
1722 if (!edl_seek_type) {
1723 if(demux_seek(demuxer, next_edl_record->stop_sec - sh_video->pts, audio_delay, 0)){
1724 sh_video->pts = demuxer->video->pts;
1725 //if (vo_vobsub) vobsub_seek(vo_vobsub,sh_video->pts);
1726 resync_video_stream(sh_video);
1727 //if(vo_spudec) spudec_reset(vo_spudec);
1728 if (audio_delay != 0.0) fixdelay(demuxer->video, d_audio, mux_a, frame_data, framecopy);
1729 return 1;
1731 // non-seekable stream.
1732 return 0;
1735 // slow seek, read every frame.
1737 return slowseek(next_edl_record->stop_sec, demuxer->video, d_audio, mux_a, frame_data, framecopy, 1);