Fix dvdnav call broken in pause changes
[mplayer.git] / mencoder.c
blobf397090e0f622d1bf7f364df3d49912a9cc96c6f
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>
40 #include "version.h"
41 #include "mp_msg.h"
42 #include "help_mp.h"
44 #include "cpudetect.h"
46 #include "codec-cfg.h"
47 #include "m_option.h"
48 #include "m_config.h"
49 #include "parser-mecmd.h"
50 #include "parser-cfg.h"
52 #include "get_path.h"
54 #include "stream/stream.h"
55 #include "libmpdemux/demuxer.h"
56 #include "libmpdemux/stheader.h"
57 #include "libmpdemux/mp3_hdr.h"
58 #include "libmpdemux/muxer.h"
61 #include "libvo/video_out.h"
63 #include "libaf/af_format.h"
65 #include "libmpcodecs/mp_image.h"
66 #include "libmpcodecs/dec_audio.h"
67 #include "libmpcodecs/dec_video.h"
68 #include "libmpcodecs/vf.h"
69 #include "libmpcodecs/vd.h"
71 // for MPEGLAYER3WAVEFORMAT:
72 #include "libmpdemux/ms_hdr.h"
74 #include <inttypes.h>
76 #include "libvo/fastmemcpy.h"
78 #include "osdep/timer.h"
80 #ifdef CONFIG_DVDREAD
81 #include "stream/stream_dvd.h"
82 #endif
84 #ifdef CONFIG_DVDNAV
85 #include "stream/stream_dvdnav.h"
86 #endif
88 #ifdef CONFIG_LIBAVCODEC
89 #include "libavcodec/avcodec.h"
90 #endif
92 #include "libmpcodecs/ae.h"
93 #include "options.h"
94 #include "defaultopts.h"
96 MPOpts opts;
97 struct osd_state *osd;
99 const int under_mencoder = 1;
100 int vo_doublebuffering=0;
101 int vo_directrendering=0;
102 int forced_subs_only=0;
104 //--------------------------
106 // cache2:
107 int stream_cache_size=-1;
108 #ifdef CONFIG_STREAM_CACHE
109 extern int cache_fill_status;
111 float stream_cache_min_percent=20.0;
112 float stream_cache_seek_min_percent=50.0;
113 #else
114 #define cache_fill_status 0
115 #endif
117 int vobsub_id=-1;
118 char* audio_lang=NULL;
119 char* dvdsub_lang=NULL;
120 static char* spudec_ifo=NULL;
122 static char** audio_codec_list=NULL; // override audio codec
123 static char** video_codec_list=NULL; // override video codec
124 static char** audio_fm_list=NULL; // override audio codec family
125 static char** video_fm_list=NULL; // override video codec family
126 extern char *demuxer_name; // override demuxer
127 extern char *audio_demuxer_name; // override audio demuxer
128 extern char *sub_demuxer_name; // override sub demuxer
130 static int out_audio_codec=-1;
131 static int out_video_codec=-1;
133 int out_file_format=MUXER_TYPE_AVI; // default to AVI
135 // audio stream skip/resync functions requires only for seeking.
136 // (they should be implemented in the audio codec layer)
137 //void skip_audio_frame(sh_audio_t *sh_audio){}
138 //void resync_audio_stream(sh_audio_t *sh_audio){}
140 int quiet=0;
141 double video_time_usage=0;
142 double vout_time_usage=0;
143 double max_video_time_usage=0;
144 double max_vout_time_usage=0;
145 double cur_video_time_usage=0;
146 double cur_vout_time_usage=0;
147 int benchmark=0;
149 #if defined(__MINGW32__) || defined(__CYGWIN__)
150 char * proc_priority=NULL;
151 #endif
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 int vo_draw_frame(struct vo *vo, uint8_t *src[]) { abort(); }
219 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y) { abort(); }
220 void vo_draw_osd(struct vo *vo, struct osd_state *osd) { abort(); }
221 void vo_flip_page(struct vo *vo) { abort(); }
222 void vo_check_events(struct vo *vo) { abort(); }
224 // Needed by getch2
225 struct mp_fifo;
226 void mplayer_put_key(struct mp_fifo *fifo, int code)
230 #ifdef CONFIG_ASS
231 #include "libass/ass.h"
232 #include "libass/ass_mp.h"
233 #endif
234 char *current_module;
235 #include "mpcommon.h"
237 //char *out_audio_codec=NULL; // override audio codec
238 //char *out_video_codec=NULL; // override video codec
240 //#include "libmpeg2/mpeg2.h"
241 //#include "libmpeg2/mpeg2_internal.h"
243 //static int vo_w=0, vo_h=0;
245 //-------------------------- config stuff:
247 m_config_t* mconfig;
249 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
251 static int cfg_include(m_option_t *conf, char *filename){
252 return m_config_parse_config_file(mconfig, filename);
255 static double seek_to_sec;
256 static off_t seek_to_byte=0;
258 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
260 static char * frameno_filename=NULL;
262 //static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width, int height);
264 typedef struct {
265 unsigned char* start;
266 int in_size;
267 float frame_time;
268 int already_read;
269 } s_frame_data;
271 /// Returns a_pts
272 static float calc_a_pts(demux_stream_t *d_audio);
273 /** \brief Seeks audio forward to pts by dumping audio packets
274 \return The current audio pts.
276 static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a);
277 /** \brief Seeks slowly by dumping frames.
278 \return 1 for success, 2 for EOF.
280 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);
281 /// Deletes audio or video as told by -delay to sync
282 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);
284 #include "edl.h"
285 static edl_record_ptr edl_records = NULL; ///< EDL entries memory area
286 static edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
287 static short edl_muted; ///< Stores whether EDL is currently in muted mode.
288 static short edl_seeking; ///< When non-zero, stream is seekable.
289 static short edl_seek_type; ///< When non-zero, frames are discarded instead of seeking.
290 /** \brief Seeks for EDL
291 \return 1 for success, 0 for failure, 2 for EOF.
293 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);
295 #include "cfg-mencoder.h"
297 #ifdef CONFIG_DVDREAD
298 #include "spudec.h"
299 #endif
300 #include "vobsub.h"
302 #include "libao2/audio_out.h"
303 /* FIXME */
304 static void mencoder_exit(int level, const char *how)
306 if (how)
307 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ExitingHow, how);
308 else
309 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_Exiting);
311 exit(level);
314 static void parse_cfgfiles( m_config_t* conf )
316 char *conffile;
317 if (!disable_system_conf &&
318 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mencoder.conf") < 0)
319 mencoder_exit(1,MSGTR_ConfigFileError);
321 if (!disable_user_conf) {
322 if ((conffile = get_path("mencoder.conf")) == NULL) {
323 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
324 } else {
325 if (m_config_parse_config_file(conf, conffile) < 0)
326 mencoder_exit(1,MSGTR_ConfigFileError);
327 free(conffile);
333 //---------------------------------------------------------------------------
335 static int dec_audio(sh_audio_t *sh_audio,unsigned char* buffer,int total){
336 int size=0;
337 int at_eof=0;
338 while(size<total && !at_eof){
339 int len=total-size;
340 if(len>MAX_OUTBURST) len=MAX_OUTBURST;
341 if (decode_audio(sh_audio, len) < 0) at_eof=1;
342 if(len>sh_audio->a_out_buffer_len) len=sh_audio->a_out_buffer_len;
343 fast_memcpy(buffer+size,sh_audio->a_out_buffer,len);
344 sh_audio->a_out_buffer_len-=len; size+=len;
345 if(sh_audio->a_out_buffer_len>0)
346 fast_memcpy(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[len],sh_audio->a_out_buffer_len);
348 return size;
351 //---------------------------------------------------------------------------
353 // this function returns the absoloute time for which MEncoder will switch files or move in the file.
354 // so audio can be cut correctly. -1 if there is no limit.
355 static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v);
357 static volatile int at_eof=0;
358 static volatile int interrupted=0;
360 static void exit_sighandler(int x){
361 at_eof=1;
362 interrupted=2; /* 1 means error */
365 static muxer_t* muxer=NULL;
367 void print_wave_header(WAVEFORMATEX *h, int verbose_level);
369 int main(int argc,char* argv[]){
371 stream_t* stream=NULL;
372 stream_t* ostream=NULL;
373 demuxer_t* demuxer=NULL;
374 stream_t* stream2=NULL;
375 demuxer_t* demuxer2=NULL;
376 demux_stream_t *d_audio=NULL;
377 demux_stream_t *d_video=NULL;
378 demux_stream_t *d_dvdsub=NULL;
379 sh_audio_t *sh_audio=NULL;
380 sh_video_t *sh_video=NULL;
381 int file_format=DEMUXER_TYPE_UNKNOWN;
382 int i=DEMUXER_TYPE_UNKNOWN;
383 void *vobsub_writer=NULL;
384 s_frame_data frame_data = { .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
386 uint32_t ptimer_start;
387 uint32_t audiorate=0;
388 uint32_t videorate=0;
389 uint32_t audiosamples=1;
390 uint32_t videosamples=1;
391 uint32_t skippedframes=0;
392 uint32_t duplicatedframes=0;
393 uint32_t badframes=0;
395 muxer_stream_t* mux_a=NULL;
396 muxer_stream_t* mux_v=NULL;
397 off_t muxer_f_size=0;
399 double v_pts_corr=0;
400 double v_timer_corr=0;
402 m_entry_t* filelist = NULL;
403 char* filename=NULL;
405 int decoded_frameno=0;
406 int next_frameno=-1;
407 int curfile=0;
408 int new_srate=0;
410 unsigned int timer_start=0;
411 ao_data_t ao_data = {0,0,0,0,OUTBURST,-1,0};
413 audio_encoding_params_t aparams;
414 audio_encoder_t *aencoder = NULL;
416 mp_msg_init();
418 // Create the config context and register the options
419 set_default_mencoder_options(&opts);
420 mconfig = m_config_new(&opts, cfg_include);
421 m_config_register_options(mconfig,mencoder_opts);
423 // Preparse the command line
424 m_config_preparse_command_line(mconfig,argc,argv);
426 mp_msg(MSGT_CPLAYER,MSGL_INFO, "MEncoder " VERSION " (C) 2000-2009 MPlayer Team\n");
428 /* Test for cpu capabilities (and corresponding OS support) for optimizing */
429 GetCpuCaps(&gCpuCaps);
430 #ifdef ARCH_X86
431 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: Type: %d MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
432 gCpuCaps.cpuType,gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
433 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
434 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
435 #ifdef RUNTIME_CPUDETECT
436 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
437 #else
438 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
439 #ifdef HAVE_MMX
440 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
441 #endif
442 #ifdef HAVE_MMX2
443 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
444 #endif
445 #ifdef HAVE_3DNOW
446 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
447 #endif
448 #ifdef HAVE_3DNOWEX
449 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
450 #endif
451 #ifdef HAVE_SSE
452 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
453 #endif
454 #ifdef HAVE_SSE2
455 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
456 #endif
457 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n\n");
458 #endif
459 #endif
461 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
462 set_path_env();
463 #endif
465 InitTimer();
467 // check codec.conf
468 if(!codecs_file || !parse_codec_cfg(codecs_file)){
469 if(!parse_codec_cfg(get_path("codecs.conf"))){
470 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
471 if(!parse_codec_cfg(NULL)){
472 mencoder_exit(1,NULL);
474 mp_msg(MSGT_MENCODER,MSGL_V,MSGTR_BuiltinCodecsConf);
479 parse_cfgfiles(mconfig);
480 filelist = m_config_parse_me_command_line(mconfig, argc, argv);
481 if(!filelist) mencoder_exit(1, MSGTR_ErrorParsingCommandLine);
484 char *extension;
486 if (!out_filename) mencoder_exit(1,MSGTR_MissingOutputFilename);
487 extension=strrchr(out_filename,'.');
488 if (extension != NULL && strlen(extension) > 3 && strlen(extension) < 6)
490 extension++;
492 switch (out_file_format)
494 case MUXER_TYPE_AVI:
495 if (strcasecmp(extension,"avi"))
496 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_MencoderWrongFormatAVI);
497 break;
499 case MUXER_TYPE_MPEG:
500 if (strcasecmp(extension,"mpg") &&
501 strcasecmp(extension,"mpeg") &&
502 strcasecmp(extension,"vob"))
503 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_MencoderWrongFormatMPG);
504 break;
508 /* Display what configure line was used */
509 mp_msg(MSGT_MENCODER, MSGL_V, "Configuration: " CONFIGURATION "\n");
512 if (frameno_filename) {
513 stream2=open_stream(frameno_filename,0,&i);
514 if(stream2){
515 demuxer2=demux_open(&opts, stream2,DEMUXER_TYPE_AVI,-1,-1,-2,NULL);
516 if(demuxer2) mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_UsingPass3ControlFile, frameno_filename);
517 else mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized);
521 #if defined(__MINGW32__) || defined(__CYGWIN__)
522 if(proc_priority){
523 int i;
524 for(i=0; priority_presets_defs[i].name; i++){
525 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
526 break;
528 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_SettingProcessPriority,
529 priority_presets_defs[i].name);
530 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
532 #endif
534 // check font
535 #ifdef CONFIG_FREETYPE
536 init_freetype();
537 #endif
538 #ifdef CONFIG_FONTCONFIG
539 if(font_fontconfig <= 0)
541 #endif
542 #ifdef CONFIG_BITMAP_FONT
543 if(font_name){
544 vo_font=read_font_desc(font_name,font_factor,verbose>1);
545 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,font_name);
546 } else {
547 // try default:
548 vo_font=read_font_desc(get_path("font/font.desc"),font_factor,verbose>1);
549 if(!vo_font)
550 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
552 #endif
553 #ifdef CONFIG_FONTCONFIG
555 #endif
557 osd = osd_create();
559 /* HACK, for some weird reason, push() has to be called twice,
560 otherwise options are not saved correctly */
561 m_config_push(mconfig);
562 play_next_file:
563 m_config_push(mconfig);
564 m_entry_set_options(mconfig,&filelist[curfile]);
565 filename = filelist[curfile].name;
567 if(!filename){
568 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_MissingFilename);
569 mencoder_exit(1,NULL);
571 stream=open_stream(filename,0,&file_format);
573 if(!stream){
574 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_CannotOpenFile_Device);
575 mencoder_exit(1,NULL);
578 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_OpenedStream, file_format, (int)(stream->start_pos), (int)(stream->end_pos));
580 #ifdef CONFIG_DVDREAD
581 if(stream->type==STREAMTYPE_DVD){
582 if(audio_lang && opts.audio_id==-1) opts.audio_id=dvd_aid_from_lang(stream,audio_lang);
583 if(dvdsub_lang && opts.sub_id==-2) opts.sub_id=dvd_sid_from_lang(stream,dvdsub_lang);
585 #endif
587 #ifdef CONFIG_DVDNAV
588 if(stream->type==STREAMTYPE_DVDNAV){
589 if(audio_lang && opts.audio_id==-1) opts.audio_id=mp_dvdnav_aid_from_lang(stream,audio_lang);
590 if(dvdsub_lang && opts.sub_id==-2) opts.sub_id=mp_dvdnav_sid_from_lang(stream,dvdsub_lang);
592 #endif
594 stream->start_pos+=seek_to_byte;
596 if(stream_cache_size>0) stream_enable_cache(stream,stream_cache_size*1024,0,0);
598 if(demuxer2) opts.audio_id=-2; /* do NOT read audio packets... */
600 //demuxer=demux_open(stream,file_format,opts.video_id,opts.audio_id,opts.sub_id);
601 demuxer=demux_open(&opts, stream,file_format,opts.audio_id,opts.video_id,opts.sub_id,filename);
602 if(!demuxer){
603 mp_msg(MSGT_DEMUXER, MSGL_FATAL, MSGTR_FormatNotRecognized);
604 mp_msg(MSGT_DEMUXER, MSGL_FATAL, MSGTR_CannotOpenDemuxer);
605 mencoder_exit(1,NULL);
608 select_audio(demuxer, opts.audio_id, audio_lang);
610 if (opts.sub_id < 0 && dvdsub_lang)
611 opts.sub_id = demuxer_sub_track_by_lang(demuxer, dvdsub_lang);
613 if (opts.sub_id < 0)
614 opts.sub_id = demuxer_default_sub_track(demuxer);
616 for (i = 0; i < MAX_S_STREAMS; i++) {
617 sh_sub_t *sh = demuxer->s_streams[i];
618 if (sh && sh->sid == opts.sub_id) {
619 demuxer->sub->id = i;
620 demuxer->sub->sh = sh;
621 break;
625 if(dvd_chapter>1) {
626 float pts;
627 if (demuxer_seek_chapter(demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
628 seek_to_sec = pts;
631 d_audio=demuxer2 ? demuxer2->audio : demuxer->audio;
632 d_video=demuxer->video;
633 d_dvdsub=demuxer->sub;
634 sh_audio=d_audio->sh;
635 sh_video=d_video->sh;
637 if(!sh_video)
639 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_VideoStreamRequired);
640 mencoder_exit(1,NULL);
643 if(!video_read_properties(sh_video)){
644 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_CannotReadVideoProperties);
645 mencoder_exit(1,NULL);
648 mp_msg(MSGT_MENCODER,MSGL_INFO, MSGTR_FilefmtFourccSizeFpsFtime,
649 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
650 sh_video->fps,sh_video->frametime
653 if(force_fps){
654 sh_video->fps=force_fps;
655 sh_video->frametime=1.0f/sh_video->fps;
656 mp_msg(MSGT_MENCODER,MSGL_INFO,MSGTR_ForcingInputFPS, sh_video->fps);
659 if(sh_audio && out_audio_codec<0){
660 if(opts.audio_id==-2)
661 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_DemuxerDoesntSupportNosound);
662 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoAudioEncoderSelected);
663 mencoder_exit(1,NULL);
665 if(sh_video && out_video_codec<0){
666 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoVideoEncoderSelected);
667 mencoder_exit(1,NULL);
670 if(sh_audio && (out_audio_codec || seek_to_sec || !sh_audio->wf || opts.playback_speed != 1.0)){
671 // Go through the codec.conf and find the best codec...
672 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
673 if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
674 sh_audio=d_audio->sh=NULL; // failed to init :(
676 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
679 if (sh_audio) {
680 new_srate = sh_audio->samplerate;
681 if (opts.playback_speed != 1.0) {
682 new_srate *= opts.playback_speed;
683 // limits are taken from libaf/af_resample.c
684 if (new_srate < 8000) new_srate = 8000;
685 if (new_srate > 192000) new_srate = 192000;
686 opts.playback_speed = (float)new_srate / (float)sh_audio->samplerate;
690 // after reading video params we should load subtitles because
691 // we know fps so now we can adjust subtitles time to ~6 seconds AST
692 // check .sub
693 // current_module="read_subtitles_file";
694 if(sub_name && sub_name[0]){
695 subdata=sub_read_file(sub_name[0], sh_video->fps);
696 if(!subdata) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,sub_name[0]);
697 } else
698 if(sub_auto && filename) { // auto load sub file ...
699 char **tmp = NULL;
700 int i = 0;
701 char *psub = get_path( "sub/" );
702 tmp = sub_filenames((psub ? psub : ""), filename);
703 free(psub);
704 subdata=sub_read_file(tmp[0], sh_video->fps);
705 while (tmp[i])
706 free(tmp[i++]);
707 free(tmp);
710 // set up video encoder:
712 if (!curfile) { // curfile is non zero when a second file is opened
713 if (vobsub_out) {
714 unsigned int palette[16], width, height;
715 unsigned char tmp[3] = { 0, 0, 0 };
716 if (spudec_ifo && vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, opts.sub_id, tmp) >= 0)
717 vobsub_writer = vobsub_out_open(vobsub_out, palette, sh_video->disp_w, sh_video->disp_h,
718 vobsub_out_id?vobsub_out_id:(char *)tmp, vobsub_out_index);
719 #ifdef CONFIG_DVDREAD
720 if (vobsub_writer == NULL) {
721 char tmp[3];
722 if (vobsub_out_id == NULL && stream->type == STREAMTYPE_DVD) {
723 int i;
724 dvd_priv_t *dvd = (dvd_priv_t*)stream->priv;
725 for (i = 0; i < dvd->nr_of_subtitles; ++i)
726 if (dvd->subtitles[i].id == opts.sub_id) {
727 tmp[0] = (dvd->subtitles[i].language >> 8) & 0xff;
728 tmp[1] = dvd->subtitles[i].language & 0xff;
729 tmp[2] = 0;
730 vobsub_out_id = tmp;
731 break;
734 vobsub_writer=vobsub_out_open(vobsub_out, stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
735 sh_video->disp_w, sh_video->disp_h, vobsub_out_id, vobsub_out_index);
737 #endif
739 else {
740 if (spudec_ifo) {
741 unsigned int palette[16], width, height;
742 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
743 vo_spudec=spudec_new_scaled(palette, sh_video->disp_w, sh_video->disp_h, NULL, 0);
745 #ifdef CONFIG_DVDREAD
746 if (vo_spudec==NULL) {
747 vo_spudec=spudec_new_scaled(stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
748 sh_video->disp_w, sh_video->disp_h, NULL, 0);
750 #endif
753 ostream = open_output_stream(out_filename, 0);
754 if(!ostream) {
755 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CannotOpenOutputFile, out_filename);
756 mencoder_exit(1,NULL);
759 muxer=muxer_new_muxer(out_file_format,ostream);
760 if(!muxer) {
761 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CannotInitializeMuxer);
762 mencoder_exit(1,NULL);
764 #if 0
765 //disabled: it horrybly distorts filtered sound
766 if(out_file_format == MUXER_TYPE_MPEG) audio_preload = 0;
767 #endif
769 muxer->audio_delay_fix = audio_delay_fix;
771 // ============= VIDEO ===============
773 mux_v=muxer_new_stream(muxer,MUXER_TYPE_VIDEO);
775 mux_v->buffer_size=0x200000; // 2MB
776 mux_v->buffer=malloc(mux_v->buffer_size);
778 mux_v->source=sh_video;
780 mux_v->h.dwSampleSize=0; // VBR
781 #ifdef CONFIG_LIBAVCODEC
783 double fps = force_ofps?force_ofps:sh_video->fps*opts.playback_speed;
784 AVRational q= av_d2q(fps, fps*1001+2);
785 mux_v->h.dwScale= q.den;
786 mux_v->h.dwRate = q.num;
788 #else
789 mux_v->h.dwScale=10000;
790 mux_v->h.dwRate=mux_v->h.dwScale*(force_ofps?force_ofps:sh_video->fps*opts.playback_speed);
791 #endif
793 mux_v->codec=out_video_codec;
795 mux_v->bih=NULL;
797 sh_video->codec=NULL;
798 sh_video->vfilter=NULL; // fixme!
800 switch(mux_v->codec){
801 case VCODEC_COPY:
802 if (!curfile) {
803 if (sh_video->bih) {
804 mux_v->bih=malloc(sh_video->bih->biSize);
805 memcpy(mux_v->bih, sh_video->bih, sh_video->bih->biSize);
807 else
809 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
810 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
811 mux_v->bih->biWidth=sh_video->disp_w;
812 mux_v->bih->biHeight=sh_video->disp_h;
813 mux_v->bih->biCompression=sh_video->format;
814 mux_v->bih->biPlanes=1;
815 mux_v->bih->biBitCount=24; // FIXME!!!
816 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
819 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
820 mux_v->bih->biWidth, mux_v->bih->biHeight,
821 mux_v->bih->biBitCount, mux_v->bih->biCompression);
823 if (curfile) {
824 if (sh_video->bih) {
825 if ((mux_v->bih->biSize != sh_video->bih->biSize) ||
826 memcmp(mux_v->bih, sh_video->bih, sh_video->bih->biSize))
828 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
829 sh_video->bih->biWidth, sh_video->bih->biHeight,
830 sh_video->bih->biBitCount, sh_video->bih->biCompression);
831 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_FrameCopyFileMismatch);
832 mencoder_exit(1,NULL);
835 else {
836 if ((mux_v->bih->biWidth != sh_video->disp_w) ||
837 (mux_v->bih->biHeight != sh_video->disp_h) ||
838 (mux_v->bih->biCompression != sh_video->format)) {
839 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
840 sh_video->disp_w, sh_video->disp_w, 24, sh_video->format);
841 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_FrameCopyFileMismatch);
842 mencoder_exit(1,NULL);
846 break;
847 case VCODEC_FRAMENO:
848 if (!curfile) {
849 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
850 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
851 mux_v->bih->biWidth=sh_video->disp_w;
852 mux_v->bih->biHeight=sh_video->disp_h;
853 mux_v->bih->biPlanes=1;
854 mux_v->bih->biBitCount=24;
855 mux_v->bih->biCompression=mmioFOURCC('F','r','N','o');
856 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
858 break;
859 default: {
860 static vf_instance_t * ve = NULL;
861 if (!ve) {
862 switch(mux_v->codec){
863 case VCODEC_LIBAVCODEC:
864 sh_video->vfilter=vf_open_encoder(&opts, NULL,"lavc",(char *)mux_v); break;
865 case VCODEC_RAW:
866 sh_video->vfilter=vf_open_encoder(&opts, NULL,"raw",(char *)mux_v); break;
867 case VCODEC_VFW:
868 sh_video->vfilter=vf_open_encoder(&opts, NULL,"vfw",(char *)mux_v); break;
869 case VCODEC_LIBDV:
870 sh_video->vfilter=vf_open_encoder(&opts, NULL,"libdv",(char *)mux_v); break;
871 case VCODEC_XVID:
872 sh_video->vfilter=vf_open_encoder(&opts, NULL,"xvid",(char *)mux_v); break;
873 case VCODEC_QTVIDEO:
874 sh_video->vfilter=vf_open_encoder(&opts, NULL,"qtvideo",(char *)mux_v); break;
875 case VCODEC_NUV:
876 sh_video->vfilter=vf_open_encoder(&opts, NULL,"nuv",(char *)mux_v); break;
877 case VCODEC_X264:
878 sh_video->vfilter=vf_open_encoder(&opts, NULL,"x264",(char *)mux_v); break;
880 if(!mux_v->bih || !sh_video->vfilter){
881 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_EncoderOpenFailed);
882 mencoder_exit(1,NULL);
884 ve = sh_video->vfilter;
885 } else sh_video->vfilter = ve;
886 // append 'expand' filter, it fixes stride problems and renders osd:
887 if (auto_expand) {
888 char* vf_args[] = { "osd", "1", NULL };
889 sh_video->vfilter=vf_open_filter(&opts, sh_video->vfilter,"expand",vf_args);
891 sh_video->vfilter=append_filters(sh_video->vfilter, opts.vf_settings);
893 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
894 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
895 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
896 if(!sh_video->initialized) mencoder_exit(1,NULL);
900 if (!curfile) {
901 /* force output fourcc to .. */
902 if ((force_fourcc != NULL) && (strlen(force_fourcc) >= 4))
904 mux_v->bih->biCompression = mmioFOURCC(force_fourcc[0], force_fourcc[1],
905 force_fourcc[2], force_fourcc[3]);
906 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ForcingOutputFourcc,
907 mux_v->bih->biCompression, (char *)&mux_v->bih->biCompression);
910 if (! ignore_start)
911 muxer->audio_delay_fix -= sh_video->stream_delay;
913 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
915 // ============= AUDIO ===============
916 if(sh_audio){
918 if (force_audiofmttag != -1) {
919 sh_audio->format = force_audiofmttag;
920 if (sh_audio->wf) {
921 sh_audio->wf->wFormatTag = sh_audio->format;
923 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ForcingOutputAudiofmtTag,
924 force_audiofmttag);
927 mux_a=muxer_new_stream(muxer,MUXER_TYPE_AUDIO);
929 mux_a->buffer_size=0x100000; //16384;
930 mux_a->buffer=malloc(mux_a->buffer_size);
931 if (!mux_a->buffer)
932 mencoder_exit(1,MSGTR_MemAllocFailed);
934 mux_a->source=sh_audio;
936 mux_a->codec=out_audio_codec;
938 ao_data.samplerate = force_srate;
939 ao_data.channels = 0;
940 ao_data.format = audio_output_format;
941 if(!init_audio_filters(sh_audio,
942 // input:
943 new_srate,
944 // output:
945 &ao_data.samplerate, &ao_data.channels, &ao_data.format)) {
946 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
947 mencoder_exit(1, NULL);
950 aparams.channels = ao_data.channels;
951 aparams.sample_rate = ao_data.samplerate;
952 aparams.audio_preload = 1000 * audio_preload;
953 if(mux_a->codec != ACODEC_COPY) {
954 aencoder = new_audio_encoder(mux_a, &aparams);
955 if(!aencoder)
956 mencoder_exit(1, NULL);
957 if(!init_audio_filters(sh_audio,
958 new_srate,
959 &aparams.sample_rate, &aparams.channels, &aencoder->input_format)) {
960 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_NoMatchingFilter);
961 mencoder_exit(1,NULL);
964 switch(mux_a->codec){
965 case ACODEC_COPY:
966 if (opts.playback_speed != 1.0) mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_NoSpeedWithFrameCopy);
967 if (sh_audio->format >= 0x10000) {
968 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantCopyAudioFormat, sh_audio->format);
969 mencoder_exit(1,NULL);
971 if (sh_audio->wf){
972 mux_a->wf=malloc(sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
973 memcpy(mux_a->wf, sh_audio->wf, sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
974 if(!sh_audio->i_bps) sh_audio->i_bps=mux_a->wf->nAvgBytesPerSec;
975 } else {
976 mux_a->wf = malloc(sizeof(WAVEFORMATEX));
977 mux_a->wf->nBlockAlign = 1; //mux_a->h.dwSampleSize;
978 mux_a->wf->wFormatTag = sh_audio->format;
979 mux_a->wf->nChannels = sh_audio->channels;
980 mux_a->wf->nSamplesPerSec = sh_audio->samplerate;
981 mux_a->wf->nAvgBytesPerSec=sh_audio->i_bps; //mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
982 mux_a->wf->wBitsPerSample = 16; // FIXME
983 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
985 if(sh_audio->audio.dwScale){
986 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize;
987 mux_a->h.dwScale=sh_audio->audio.dwScale;
988 mux_a->h.dwRate=sh_audio->audio.dwRate;
989 } else {
990 mux_a->h.dwSampleSize=mux_a->wf->nBlockAlign;
991 mux_a->h.dwScale=mux_a->h.dwSampleSize;
992 mux_a->h.dwRate=mux_a->wf->nAvgBytesPerSec;
994 mux_a->h.dwRate *= opts.playback_speed;
995 mux_a->wf->nSamplesPerSec *= opts.playback_speed;
996 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
997 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
998 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
999 break;
1002 if ( mp_msg_test(MSGT_MENCODER,MSGL_DBG2) ) print_wave_header(mux_a->wf, MSGL_DBG2);
1004 if (! ignore_start)
1005 muxer->audio_delay_fix += sh_audio->stream_delay;
1007 } // if(sh_audio)
1009 decoded_frameno=0;
1011 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
1012 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
1013 signal(SIGTERM,exit_sighandler); // kill
1014 signal(SIGHUP,exit_sighandler); // broken terminal line
1015 signal(SIGPIPE,exit_sighandler); // broken pipe
1017 timer_start=GetTimerMS();
1018 } // if (!curfile) // if this was the first file.
1019 else {
1020 if (!mux_a != !sh_audio) {
1021 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoAudioFileMismatch);
1022 mencoder_exit(1,NULL);
1024 if (sh_audio && mux_a->codec == ACODEC_COPY) {
1025 if (opts.playback_speed != 1.0) mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_NoSpeedWithFrameCopy);
1026 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
1027 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
1028 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
1029 if (sh_audio->wf) {
1030 if ((mux_a->wf->wFormatTag != sh_audio->wf->wFormatTag) ||
1031 (mux_a->wf->nChannels != sh_audio->wf->nChannels) ||
1032 (mux_a->wf->nSamplesPerSec != sh_audio->wf->nSamplesPerSec * opts.playback_speed))
1034 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
1035 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * opts.playback_speed),
1036 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1037 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_AudioCopyFileMismatch);
1038 mencoder_exit(1,NULL);
1040 } else {
1041 if ((mux_a->wf->wFormatTag != sh_audio->format) ||
1042 (mux_a->wf->nChannels != sh_audio->channels) ||
1043 (mux_a->wf->nSamplesPerSec != sh_audio->samplerate * opts.playback_speed))
1045 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
1046 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * opts.playback_speed),
1047 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1048 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_AudioCopyFileMismatch);
1049 mencoder_exit(1,NULL);
1052 } else if (sh_audio) {
1053 int out_srate = mux_a->wf->nSamplesPerSec;
1054 int out_channels = mux_a->wf->nChannels;
1055 int out_format = aencoder->input_format;
1056 if (!init_audio_filters(sh_audio, new_srate,
1057 &out_srate, &out_channels,
1058 &out_format)) {
1059 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoMatchingFilter);
1060 mencoder_exit(1, NULL);
1062 mux_a->wf->nSamplesPerSec = out_srate;
1063 mux_a->wf->nChannels = out_channels;
1067 if (seek_to_sec) {
1068 demux_seek(demuxer, seek_to_sec, audio_delay, 1);
1069 // there is 2 way to handle the -ss option in 3-pass mode:
1070 // > 1. do the first pass for the whole file, and use -ss for 2nd/3rd pases only
1071 // > 2. do all the 3 passes with the same -ss value
1072 // this line enables behaviour 1. (and kills 2. at the same time):
1073 // if(demuxer2) demux_seek(demuxer2, d, audio_delay, 1);
1076 if (out_file_format == MUXER_TYPE_MPEG)
1078 if (audio_preload > 0.4) {
1079 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_LimitingAudioPreload);
1080 audio_preload = 0.4;
1082 if (audio_density < 4) {
1083 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_IncreasingAudioDensity);
1084 audio_density = 4;
1088 if(file_format == DEMUXER_TYPE_TV)
1090 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_ZeroingAudioPreloadAndMaxPtsCorrection);
1091 audio_preload = 0.0;
1092 default_max_pts_correction = 0;
1095 play_n_frames=play_n_frames_mf;
1096 if (curfile && end_at.type == END_AT_TIME) end_at.pos += mux_v->timer;
1098 if (edl_records) free_edl(edl_records);
1099 next_edl_record = edl_records = NULL;
1100 edl_muted = 0;
1101 edl_seeking = 1;
1102 if (edl_filename) {
1103 next_edl_record = edl_records = edl_parse_file();
1106 if (sh_audio && audio_delay != 0.) fixdelay(d_video, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1108 while(!at_eof){
1110 int blit_frame=0;
1111 float a_pts=0;
1112 float v_pts=0;
1113 int skip_flag=0; // 1=skip -1=duplicate
1115 if((end_at.type == END_AT_SIZE && end_at.pos <= stream_tell(muxer->stream)) ||
1116 (end_at.type == END_AT_TIME && end_at.pos < mux_v->timer))
1117 break;
1119 if(play_n_frames>=0){
1120 --play_n_frames;
1121 if(play_n_frames<0) break;
1123 if(dvd_last_chapter>0) {
1124 int cur_chapter = demuxer_get_current_chapter(demuxer);
1125 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
1126 break;
1129 goto_redo_edl:
1130 if (next_edl_record && sh_video && sh_video->pts >= next_edl_record->start_sec) {
1131 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1132 float last_pos = d_video->pts;
1133 int result;
1134 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop [%f], length [%f]\n",
1135 next_edl_record->start_sec, next_edl_record->stop_sec, next_edl_record->length_sec);
1137 result = edl_seek(next_edl_record, demuxer, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1139 if (result == 2) { at_eof=1; break; } // EOF
1140 else if (result == 0) edl_seeking = 0; // no seeking
1141 else { // sucess
1142 edl_muted = 0;
1143 if (last_pos >= sh_video->pts) {
1144 // backwards seek detected!! Forget about this EDL skip altogether.
1145 next_edl_record = next_edl_record->next;
1147 else for (next_edl_record = edl_records; next_edl_record; next_edl_record = next_edl_record->next) {
1148 /* note the use of stop_sec,
1149 meaning if by some magical way we landed in the MIDDLE of a censored area,
1150 in the next loop it will jump out of it.
1152 if (next_edl_record->stop_sec > sh_video->pts) break; // we got to the right place.
1153 if (next_edl_record->action == EDL_MUTE) edl_muted = !edl_muted; // toggle mute each time.
1156 /* for a pedantic EDL, that doesn't show even a single
1157 frame from the "censored" area, uncomment next line. */
1158 goto goto_redo_edl;
1160 } else if (next_edl_record->action == EDL_MUTE) {
1161 edl_muted = !edl_muted; // This variable does nothing for now.
1162 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n", next_edl_record->start_sec );
1163 next_edl_record=next_edl_record->next;
1168 if(sh_audio){
1169 // get audio:
1170 while(mux_a->timer-audio_preload<mux_v->timer){
1171 float tottime;
1172 int len=0;
1174 ptimer_start = GetTimerMS();
1175 // CBR - copy 0.5 sec of audio
1176 // or until the end of video:
1177 tottime = stop_time(demuxer, mux_v);
1178 if (tottime != -1) {
1179 tottime -= mux_a->timer;
1180 if (tottime > 1./audio_density) tottime = 1./audio_density;
1182 else tottime = 1./audio_density;
1184 // let's not output more audio than necessary
1185 if (tottime <= 0) break;
1187 if(aencoder)
1189 if(mux_a->h.dwSampleSize) /* CBR */
1191 if(aencoder->set_decoded_len)
1193 len = mux_a->h.dwSampleSize*(int)(mux_a->h.dwRate*tottime);
1194 aencoder->set_decoded_len(aencoder, len);
1196 else
1197 len = aencoder->decode_buffer_size;
1199 len = dec_audio(sh_audio, aencoder->decode_buffer, len);
1200 mux_a->buffer_len += aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len,
1201 aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1202 if(mux_a->buffer_len < mux_a->wf->nBlockAlign)
1203 len = 0;
1204 else
1205 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1207 else /* VBR */
1209 int sz = 0;
1210 while(1)
1212 len = 0;
1213 if(! sz)
1214 sz = aencoder->get_frame_size(aencoder);
1215 if(sz > 0 && mux_a->buffer_len >= sz)
1217 len = sz;
1218 break;
1220 len = dec_audio(sh_audio,aencoder->decode_buffer, aencoder->decode_buffer_size);
1221 if(len <= 0)
1223 len = 0;
1224 break;
1226 len = aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len, aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1227 mux_a->buffer_len += len;
1230 if (mux_v->timer == 0) mux_a->h.dwInitialFrames++;
1232 else {
1233 if(mux_a->h.dwSampleSize){
1234 switch(mux_a->codec){
1235 case ACODEC_COPY: // copy
1236 len=mux_a->wf->nAvgBytesPerSec*tottime;
1237 len/=mux_a->h.dwSampleSize;if(len<1) len=1;
1238 len*=mux_a->h.dwSampleSize;
1239 len=demux_read_data(sh_audio->ds,mux_a->buffer,len);
1240 break;
1242 } else {
1243 // VBR - encode/copy an audio frame
1244 switch(mux_a->codec){
1245 case ACODEC_COPY: // copy
1246 len=ds_get_packet(sh_audio->ds,(unsigned char**) &mux_a->buffer);
1247 break;
1251 if(len<=0) break; // EOF?
1252 muxer_write_chunk(mux_a,len,0x10, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1253 if(!mux_a->h.dwSampleSize && mux_a->timer>0)
1254 mux_a->wf->nAvgBytesPerSec=0.5f+(double)mux_a->size/mux_a->timer; // avg bps (VBR)
1255 if(mux_a->buffer_len>=len){
1256 mux_a->buffer_len-=len;
1257 fast_memcpy(mux_a->buffer,mux_a->buffer+len,mux_a->buffer_len);
1261 audiosamples++;
1262 audiorate+= (GetTimerMS() - ptimer_start);
1267 // get video frame!
1269 if (!frame_data.already_read) {
1270 frame_data.in_size=video_read_frame(sh_video,&frame_data.frame_time,&frame_data.start,force_fps);
1271 sh_video->timer+=frame_data.frame_time;
1273 frame_data.frame_time /= opts.playback_speed;
1274 if(frame_data.in_size<0){ at_eof=1; break; }
1275 ++decoded_frameno;
1277 v_timer_corr-=frame_data.frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate;
1279 if(demuxer2){ // 3-pass encoding, read control file (frameno.avi)
1280 // find our frame:
1281 while(next_frameno<decoded_frameno){
1282 int* start;
1283 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1284 if(len<0){ at_eof=1;break;}
1285 if(len==0) --skip_flag; else // duplicate
1286 if(len==4) next_frameno=start[0];
1288 if(at_eof) break;
1289 // if(skip_flag) printf("!!!!!!!!!!!!\n");
1290 skip_flag=next_frameno-decoded_frameno;
1291 // find next frame:
1292 while(next_frameno<=decoded_frameno){
1293 int* start;
1294 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1295 if(len<0){ at_eof=1;break;}
1296 if(len==0) --skip_flag; else // duplicate
1297 if(len==4) next_frameno=start[0];
1299 // if(at_eof) break;
1300 // printf("Current fno=%d requested=%d skip=%d \n",decoded_frameno,fno,skip_flag);
1301 } else {
1303 // check frame duplicate/drop:
1305 //printf("\r### %5.3f ###\n",v_timer_corr);
1306 float mux_frametime = (float)mux_v->h.dwScale/mux_v->h.dwRate;
1308 if (v_timer_corr >= mux_frametime && (skip_limit<0 || skip_flag < skip_limit)) {
1309 v_timer_corr-=mux_frametime;
1310 ++skip_flag; // skip
1312 while (v_timer_corr <= -mux_frametime && (skip_limit<0 || -skip_flag < skip_limit)) {
1313 v_timer_corr+=mux_frametime;
1314 --skip_flag; // dup
1317 // either v_pts_corr is big, more than 2 times framerate, then we follow its advice,
1318 // or, it cancels out v_timer_corr, in which case be happy and do nothing.
1320 while ((v_pts_corr <= -mux_frametime && skip_flag > 0) || (v_pts_corr <= -2*mux_frametime)) {
1321 v_pts_corr+=mux_frametime;
1322 --skip_flag; // dup
1324 if ((v_pts_corr >= mux_frametime && skip_flag < 0) || (v_pts_corr >= 2*mux_frametime)) {
1325 if (skip_flag<=0) { // we can't skip more than 1 frame now
1326 v_pts_corr-=mux_frametime;
1327 ++skip_flag; // skip
1331 } // demuxer2
1333 ptimer_start = GetTimerMS();
1335 switch(mux_v->codec){
1336 case VCODEC_COPY:
1337 mux_v->buffer=frame_data.start;
1338 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);
1339 break;
1340 case VCODEC_FRAMENO:
1341 mux_v->buffer=(unsigned char *)&decoded_frameno; // tricky
1342 if(skip_flag<=0) muxer_write_chunk(mux_v,sizeof(int),0x10, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1343 break;
1344 default:
1345 // decode_video will callback down to ve_*.c encoders, through the video filters
1346 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ, osd);
1347 {void *decoded_frame = decode_video(sh_video,frame_data.start,frame_data.in_size,
1348 skip_flag>0 && (!sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) != CONTROL_TRUE), MP_NOPTS_VALUE);
1349 blit_frame = decoded_frame && filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE);
1350 if (blit_frame) {
1351 struct vf_instance *vf = sh_video->vfilter;
1352 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
1353 vf->control(vf, VFCTRL_DRAW_OSD, osd);
1357 if (sh_video->vf_initialized < 0) mencoder_exit(1, NULL);
1359 if(!blit_frame){
1360 if (play_n_frames >= 0)
1361 play_n_frames++;
1362 badframes++;
1363 if(skip_flag<=0){
1364 // unwanted skipping of a frame, what to do?
1365 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1366 #if 0
1367 // Old code apparently made under the assumption that !blit_frame means
1368 // decoding failed due to corruption or something.. but duplication and
1369 // skipping of frames should be entirely disabled when skip_limit==0,
1370 // and must be in order for many filters to work with -noskip.
1371 // Eventually this entire block should probably be removed.
1372 if(skip_limit==0){
1373 // skipping not allowed -> write empty frame:
1374 if (!encode_duplicates || !sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1375 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1376 } else {
1377 // skipping allowed -> skip it and distriubute timer error:
1378 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1380 #endif
1385 videosamples++;
1386 videorate+=(GetTimerMS() - ptimer_start);
1388 if(skip_flag<0){
1389 // duplicate frame
1390 if(!quiet) mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_DuplicateFrames,-skip_flag);
1391 while(skip_flag<0){
1392 duplicatedframes++;
1393 if (!encode_duplicates || !sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1394 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1395 ++skip_flag;
1397 } else
1398 if(skip_flag>0){
1399 // skip frame
1400 if(!quiet) mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_SkipFrame);
1401 skippedframes++;
1402 --skip_flag;
1405 if(sh_audio && !demuxer2){
1406 float AV_delay,x;
1407 // A-V sync!
1408 #if 0
1409 if(pts_from_bps){
1410 unsigned int samples=(sh_audio->audio.dwSampleSize)?
1411 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
1412 (d_audio->block_no); // <- used for VBR audio
1413 // printf("samples=%d \n",samples);
1414 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
1415 delay_corrected=1;
1416 } else
1417 #endif
1419 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
1420 a_pts=d_audio->pts;
1421 if(!delay_corrected) if(a_pts) delay_corrected=1;
1422 //printf("*** %5.3f ***\n",a_pts);
1423 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1425 v_pts=sh_video ? sh_video->pts : d_video->pts;
1426 // av = compensated (with out buffering delay) A-V diff
1427 AV_delay=(a_pts-v_pts);
1428 AV_delay-=audio_delay;
1429 AV_delay /= opts.playback_speed;
1430 AV_delay-=mux_a->timer-(mux_v->timer-(v_timer_corr+v_pts_corr));
1431 // adjust for encoder delays
1432 AV_delay -= (float) mux_a->encoder_delay * mux_a->h.dwScale/mux_a->h.dwRate;
1433 AV_delay += (float) mux_v->encoder_delay * mux_v->h.dwScale/mux_v->h.dwRate;
1434 // compensate input video timer by av:
1435 x=AV_delay*0.1f;
1436 if(x<-max_pts_correction) x=-max_pts_correction; else
1437 if(x> max_pts_correction) x= max_pts_correction;
1438 if(default_max_pts_correction>=0)
1439 max_pts_correction=default_max_pts_correction*opts.playback_speed;
1440 else
1441 max_pts_correction=sh_video->frametime*0.10 *opts.playback_speed; // +-10% of time
1442 // sh_video->timer-=x;
1443 c_total+=x;
1444 v_pts_corr+=x;
1447 // printf("A:%6.1f V:%6.1f A-V:%7.3f oAV:%7.3f diff:%7.3f ct:%7.3f vpc:%7.3f \r",
1448 // a_pts,v_pts,a_pts-v_pts,
1449 // (float)(mux_a->timer-mux_v->timer),
1450 // AV_delay, c_total, v_pts_corr );
1451 // printf("V:%6.1f \r", d_video->pts );
1453 #if 0
1454 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",
1455 a_pts,v_pts,a_pts-v_pts,c_total,
1456 (int)sh_video->num_frames,(int)sh_video->num_frames_decoded,
1457 (sh_video->timer>0.5)?(int)(100.0*video_time_usage/(double)sh_video->timer):0,
1458 (sh_video->timer>0.5)?(int)(100.0*vout_time_usage/(double)sh_video->timer):0,
1459 (sh_video->timer>0.5)?(100.0*audio_time_usage/(double)sh_video->timer):0
1460 ,cache_fill_status
1462 #endif
1464 { float t=(GetTimerMS()-timer_start)*0.001f;
1465 float len=(demuxer->movi_end-demuxer->movi_start);
1466 float p=len>1000 ? (float)(demuxer->filepos-demuxer->movi_start) / len :
1467 (demuxer_get_percent_pos(demuxer) / 100.0);
1468 #if 0
1469 if(!len && sh_audio && sh_audio->audio.dwLength>100){
1470 p=(sh_audio->audio.dwSampleSize? ds_tell(sh_audio->ds)/sh_audio->audio.dwSampleSize : sh_audio->ds->block_no)
1471 / (float)(sh_audio->audio.dwLength);
1473 #endif
1474 #if 0
1475 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"%d < %d < %d \r",
1476 (int)demuxer->movi_start,
1477 (int)demuxer->filepos,
1478 (int)demuxer->movi_end);
1479 #else
1480 if(!quiet) {
1481 if( mp_msg_test(MSGT_STATUSLINE,MSGL_V) ) {
1482 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",
1483 mux_v->timer, decoded_frameno, (int)(p*100),
1484 (t>1) ? (int)(decoded_frameno/t+0.5) : 0,
1485 (p>0.001) ? (int)((t/p-t)/60) : 0,
1486 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1487 v_pts_corr,
1488 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1489 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0,
1490 audiorate/audiosamples, videorate/videosamples,
1491 duplicatedframes, badframes, skippedframes
1493 } else
1494 mp_msg(MSGT_STATUSLINE,MSGL_STATUS,"Pos:%6.1fs %6df (%2d%%) %5.2ffps Trem:%4dmin %3dmb A-V:%5.3f [%d:%d]\r",
1495 mux_v->timer, decoded_frameno, (int)(p*100),
1496 (t>1) ? (float)(decoded_frameno/t) : 0,
1497 (p>0.001) ? (int)((t/p-t)/60) : 0,
1498 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1499 v_pts_corr,
1500 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1501 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0
1504 #endif
1506 fflush(stdout);
1508 #ifdef CONFIG_DVDREAD
1509 // DVD sub:
1510 if(vobsub_writer){
1511 unsigned char* packet=NULL;
1512 int len;
1513 while((len=ds_get_packet_sub(d_dvdsub,&packet))>0){
1514 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);
1515 vobsub_out_output(vobsub_writer,packet,len,mux_v->timer + d_dvdsub->pts - sh_video->pts);
1518 else
1519 #endif
1520 update_subtitles(sh_video, d_dvdsub, 0);
1522 frame_data = (s_frame_data){ .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
1524 #if 0
1525 if(ferror(muxer_f)) {
1526 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile, out_filename);
1527 mencoder_exit(1, NULL);
1529 #endif
1531 } // while(!at_eof)
1533 if (!interrupted && filelist[++curfile].name != 0) {
1534 if (sh_video && sh_video->vfilter) { // Before uniniting sh_video and the filter chain, break apart the VE.
1535 vf_instance_t * ve; // this will be the filter right before the ve.
1536 for (ve = sh_video->vfilter; ve->next && ve->next->next; ve = ve->next);
1538 if (ve->next) ve->next = NULL; // I'm telling the last filter, before the VE, there is nothing after it
1539 else sh_video->vfilter = NULL; // There is no chain except the VE.
1542 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1543 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1544 if(demuxer) free_demuxer(demuxer);
1545 if(stream) free_stream(stream); // kill cache thread
1547 at_eof = 0;
1549 m_config_pop(mconfig);
1550 goto play_next_file;
1553 /* Emit the remaining frames in the video system */
1554 /*TODO emit frmaes delayed by decoder lag*/
1555 if(sh_video && sh_video->vfilter){
1556 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_FlushingVideoFrames);
1557 if (!sh_video->vfilter->fmt.have_configured)
1558 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_FiltersHaveNotBeenConfiguredEmptyFile);
1559 else
1560 sh_video->vfilter->control(sh_video->vfilter,
1561 VFCTRL_FLUSH_FRAMES, 0);
1564 if(aencoder)
1565 if(aencoder->fixup)
1566 aencoder->fixup(aencoder);
1568 if (muxer->cont_write_index) muxer_write_index(muxer);
1569 muxer_f_size=stream_tell(muxer->stream);
1570 stream_seek(muxer->stream,0);
1571 if (muxer->cont_write_header) muxer_write_header(muxer); // update header
1572 #if 0
1573 if(ferror(muxer_f) || fclose(muxer_f) != 0) {
1574 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile, out_filename);
1575 mencoder_exit(1, NULL);
1577 #endif
1578 if(vobsub_writer)
1579 vobsub_out_close(vobsub_writer);
1581 if(out_video_codec==VCODEC_FRAMENO && mux_v->timer>100){
1582 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"650MB",(int)((650*1024*1024-muxer_f_size)/mux_v->timer/125));
1583 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"700MB",(int)((700*1024*1024-muxer_f_size)/mux_v->timer/125));
1584 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"800MB",(int)((800*1024*1024-muxer_f_size)/mux_v->timer/125));
1585 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"2 x 650MB",(int)((2*650*1024*1024-muxer_f_size)/mux_v->timer/125));
1586 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"2 x 700MB",(int)((2*700*1024*1024-muxer_f_size)/mux_v->timer/125));
1587 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"2 x 800MB",(int)((2*800*1024*1024-muxer_f_size)/mux_v->timer/125));
1590 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VideoStreamResult,
1591 (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);
1592 if(sh_audio)
1593 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_AudioStreamResult,
1594 (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);
1596 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1597 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1598 if(demuxer) free_demuxer(demuxer);
1599 if(stream) free_stream(stream); // kill cache thread
1601 return interrupted;
1605 #if 0
1606 /* Flip the image in src and store the result in dst. src and dst may overlap.
1607 width is the size of each line in bytes. */
1608 static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width,
1609 int height)
1611 uint8_t* tmp = malloc(width);
1612 int i;
1614 for(i = 0; i < height/2; i++) {
1615 fast_memcpy(tmp, &src[i*width], width);
1616 fast_memcpy(&dst[i * width], &src[(height - i) * width], width);
1617 fast_memcpy(&dst[(height - i) * width], tmp, width);
1620 free(tmp);
1621 return dst;
1623 #endif
1626 static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v) {
1627 float timeleft = -1;
1628 if (play_n_frames >= 0) timeleft = mux_v->timer + play_n_frames * (double)(mux_v->h.dwScale) / mux_v->h.dwRate;
1629 if (end_at.type == END_AT_TIME && (timeleft > end_at.pos || timeleft == -1)) timeleft = end_at.pos;
1630 if (next_edl_record && demuxer && demuxer->video) { // everything is OK to be checked
1631 float tmp = mux_v->timer + next_edl_record->start_sec - demuxer->video->pts;
1632 if (timeleft == -1 || timeleft > tmp) {
1633 // There's less time in EDL than what we already know
1634 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1635 timeleft = tmp;
1636 } else if (next_edl_record->action == EDL_MUTE) {
1637 //timeleft = next_edl_record->start_sec - demuxer->video->pts;
1638 // For the moment (and probably forever) EDL mute doesn't work in MEncoder
1642 return timeleft;
1645 static float calc_a_pts(demux_stream_t *d_audio) {
1646 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
1647 float a_pts = 0.;
1648 if (sh_audio)
1649 a_pts = d_audio->pts + (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1650 return a_pts;
1653 static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a) {
1654 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
1655 int samplesize, avg;
1656 float a_pts = calc_a_pts(d_audio);
1658 if (!sh_audio) return a_pts;
1660 if (sh_audio->audio.dwScale) samplesize = sh_audio->audio.dwSampleSize;
1661 else samplesize = (sh_audio->wf ? sh_audio->wf->nBlockAlign : 1);
1662 avg = (sh_audio->wf ? sh_audio->wf->nAvgBytesPerSec : sh_audio->i_bps);
1664 // after a demux_seek, a_pts will be zero until you read some audio.
1665 // carefully checking if a_pts is truely correct by reading tiniest amount of data possible.
1666 if (pts > a_pts && a_pts == 0.0 && samplesize) {
1667 if (demux_read_data(sh_audio->ds,mux_a->buffer,samplesize) <= 0) return a_pts; // EOF
1668 a_pts = calc_a_pts(d_audio);
1671 while (pts > a_pts) {
1672 int len;
1673 if (samplesize) {
1674 len = avg * (pts - a_pts > 0.5 ? 0.5 : pts - a_pts);
1675 len/= samplesize; if(len<1) len=1;
1676 len*= samplesize;
1677 len = demux_read_data(sh_audio->ds,mux_a->buffer,len);
1678 } else {
1679 unsigned char * crap;
1680 len = ds_get_packet(sh_audio->ds, &crap);
1682 if (len <= 0) break; // EOF of audio.
1683 a_pts = calc_a_pts(d_audio);
1685 return a_pts;
1688 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) {
1689 sh_video_t * sh_video = d_video->sh;
1690 vf_instance_t * vfilter = sh_video ? sh_video->vfilter : NULL;
1691 int done = 0;
1693 while (!interrupted) {
1694 float a_pts = 0.;
1696 if (!frame_data->already_read) { // when called after fixdelay, a frame is already read
1697 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
1698 if(frame_data->in_size<0) return 2;
1699 sh_video->timer += frame_data->frame_time;
1701 frame_data->already_read = 0;
1703 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
1705 if (done) {
1706 // wait for keyframe in case of -ovc copy
1707 if (!framecopy || (sh_video->ds->flags & 1)) {
1708 frame_data->already_read = 1;
1709 return 1;
1712 if (sh_video->pts >= end_pts) done = 1;
1714 if (vfilter) {
1715 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
1716 osd);
1717 int softskip = (vfilter->control(vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) == CONTROL_TRUE);
1718 void *decoded_frame = decode_video(sh_video, frame_data->start, frame_data->in_size, !softskip, MP_NOPTS_VALUE);
1719 if (decoded_frame)
1720 if (filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE)) {
1721 struct vf_instance *vf = sh_video->vfilter;
1722 vf->control(vf, VFCTRL_DRAW_EOSD, NULL);
1723 vf->control(vf, VFCTRL_DRAW_OSD, osd);
1727 if (print_info) mp_msg(MSGT_MENCODER, MSGL_STATUS,
1728 MSGTR_EdlSkipStartEndCurrent,
1729 next_edl_record->start_sec, next_edl_record->stop_sec,
1730 sh_video->pts, a_pts);
1732 if (interrupted) return 2;
1733 return 1;
1736 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) {
1737 // TODO: Find a way to encode silence instead of deleting video
1738 sh_video_t * sh_video = d_video->sh;
1739 float a_pts;
1741 // demux_seek has a weirdness that sh_video->pts is meaningless,
1742 // until a single frame is read... Same for audio actually too.
1743 // Reading one frame, and keeping it.
1744 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
1745 if(frame_data->in_size<0) return;
1746 sh_video->timer += frame_data->frame_time;
1747 frame_data->already_read = 1;
1749 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
1751 if (audio_delay > 0) return;
1752 else if (sh_video->pts - frame_data->frame_time + audio_delay >= a_pts) return;
1754 slowseek(a_pts - audio_delay, d_video, d_audio, mux_a, frame_data, framecopy, 0);
1757 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) {
1758 sh_video_t * sh_video = demuxer->video ? demuxer->video->sh : NULL;
1760 if (!sh_video) return 0;
1761 if (sh_video->pts >= next_edl_record->stop_sec) return 1; // nothing to do...
1763 if (!edl_seek_type) {
1764 if(demux_seek(demuxer, next_edl_record->stop_sec - sh_video->pts, audio_delay, 0)){
1765 sh_video->pts = demuxer->video->pts;
1766 //if (vo_vobsub) vobsub_seek(vo_vobsub,sh_video->pts);
1767 resync_video_stream(sh_video);
1768 //if(vo_spudec) spudec_reset(vo_spudec);
1769 if (audio_delay != 0.0) fixdelay(demuxer->video, d_audio, mux_a, frame_data, framecopy);
1770 return 1;
1772 // non-seekable stream.
1773 return 0;
1776 // slow seek, read every frame.
1778 return slowseek(next_edl_record->stop_sec, demuxer->video, d_audio, mux_a, frame_data, framecopy, 1);