Use correct terminal "clear line" code
[mplayer.git] / mencoder.c
blob4a8575e75d725ab6152acc423024f81861ddd752
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 #ifdef WIN32
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 int vo_doublebuffering=0;
100 int vo_directrendering=0;
101 int forced_subs_only=0;
103 //--------------------------
105 // cache2:
106 int stream_cache_size=-1;
107 #ifdef CONFIG_STREAM_CACHE
108 extern int cache_fill_status;
110 float stream_cache_min_percent=20.0;
111 float stream_cache_seek_min_percent=50.0;
112 #else
113 #define cache_fill_status 0
114 #endif
116 int vobsub_id=-1;
117 char* audio_lang=NULL;
118 char* dvdsub_lang=NULL;
119 static char* spudec_ifo=NULL;
121 static char** audio_codec_list=NULL; // override audio codec
122 static char** video_codec_list=NULL; // override video codec
123 static char** audio_fm_list=NULL; // override audio codec family
124 static char** video_fm_list=NULL; // override video codec family
125 extern char *demuxer_name; // override demuxer
126 extern char *audio_demuxer_name; // override audio demuxer
127 extern char *sub_demuxer_name; // override sub demuxer
129 static int out_audio_codec=-1;
130 static int out_video_codec=-1;
132 int out_file_format=MUXER_TYPE_AVI; // default to AVI
134 // audio stream skip/resync functions requires only for seeking.
135 // (they should be implemented in the audio codec layer)
136 //void skip_audio_frame(sh_audio_t *sh_audio){}
137 //void resync_audio_stream(sh_audio_t *sh_audio){}
139 int quiet=0;
140 double video_time_usage=0;
141 double vout_time_usage=0;
142 double max_video_time_usage=0;
143 double max_vout_time_usage=0;
144 double cur_video_time_usage=0;
145 double cur_vout_time_usage=0;
146 int benchmark=0;
148 #ifdef WIN32
149 char * proc_priority=NULL;
150 #endif
152 // A-V sync:
153 int delay_corrected=1;
154 static float default_max_pts_correction=-1;//0.01f;
155 static float max_pts_correction=0;//default_max_pts_correction;
156 static float c_total=0;
158 static float audio_preload=0.5;
159 static float audio_delay_fix=0.0;
160 static float audio_delay=0.0;
161 static int ignore_start=0;
162 static int audio_density=2;
164 double force_fps=0;
165 static double force_ofps=0; // set to 24 for inverse telecine
166 static int skip_limit=-1;
168 static int force_srate=0;
169 static int audio_output_format=0;
171 char *vobsub_out=NULL;
172 unsigned int vobsub_out_index=0;
173 char *vobsub_out_id=NULL;
175 char* out_filename=NULL;
177 char *force_fourcc=NULL;
178 int force_audiofmttag=-1;
180 char* passtmpfile="divx2pass.log";
182 static int play_n_frames=-1;
183 static int play_n_frames_mf=-1;
185 #include "libvo/font_load.h"
186 #include "libvo/sub.h"
188 // sub:
189 char *font_name=NULL;
190 char *sub_font_name=NULL;
191 extern int font_fontconfig;
192 float font_factor=0.75;
193 char **sub_name=NULL;
194 float sub_delay=0;
195 float sub_fps=0;
196 int sub_auto = 0;
197 int subcc_enabled=0;
198 int suboverlap_enabled = 1;
200 int auto_expand=1;
201 int encode_duplicates=1;
203 // infos are empty by default
204 char *info_name=NULL;
205 char *info_artist=NULL;
206 char *info_genre=NULL;
207 char *info_subject=NULL;
208 char *info_copyright=NULL;
209 char *info_sourceform=NULL;
210 char *info_comment=NULL;
212 // Needed by libmpcodecs vf_vo.c
213 int vo_config(struct vo *vo, uint32_t width, uint32_t height,
214 uint32_t d_width, uint32_t d_height, uint32_t flags,
215 char *title, uint32_t format) { abort(); }
216 int vo_control(struct vo *vo, uint32_t request, void *data) { abort(); }
217 int vo_draw_frame(struct vo *vo, uint8_t *src[]) { abort(); }
218 int vo_draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h, int x, int y) { abort(); }
219 void vo_draw_osd(struct vo *vo, struct osd_state *osd) { abort(); }
220 void vo_flip_page(struct vo *vo) { abort(); }
221 void vo_check_events(struct vo *vo) { abort(); }
223 // Needed by getch2
224 struct mp_fifo;
225 void mplayer_put_key(struct mp_fifo *fifo, int code)
229 #ifdef CONFIG_ASS
230 #include "libass/ass.h"
231 #include "libass/ass_mp.h"
232 #endif
233 char *current_module;
234 #include "mpcommon.h"
236 //char *out_audio_codec=NULL; // override audio codec
237 //char *out_video_codec=NULL; // override video codec
239 //#include "libmpeg2/mpeg2.h"
240 //#include "libmpeg2/mpeg2_internal.h"
242 //static int vo_w=0, vo_h=0;
244 //-------------------------- config stuff:
246 m_config_t* mconfig;
248 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
250 static int cfg_include(m_option_t *conf, char *filename){
251 return m_config_parse_config_file(mconfig, filename);
254 static double seek_to_sec;
255 static off_t seek_to_byte=0;
257 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
259 static char * frameno_filename=NULL;
261 //static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width, int height);
263 typedef struct {
264 unsigned char* start;
265 int in_size;
266 float frame_time;
267 int already_read;
268 } s_frame_data;
270 /// Returns a_pts
271 static float calc_a_pts(demux_stream_t *d_audio);
272 /** \brief Seeks audio forward to pts by dumping audio packets
273 \return The current audio pts.
275 static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a);
276 /** \brief Seeks slowly by dumping frames.
277 \return 1 for success, 2 for EOF.
279 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);
280 /// Deletes audio or video as told by -delay to sync
281 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);
283 #include "edl.h"
284 static edl_record_ptr edl_records = NULL; ///< EDL entries memory area
285 static edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
286 static short edl_muted; ///< Stores whether EDL is currently in muted mode.
287 static short edl_seeking; ///< When non-zero, stream is seekable.
288 static short edl_seek_type; ///< When non-zero, frames are discarded instead of seeking.
289 /** \brief Seeks for EDL
290 \return 1 for success, 0 for failure, 2 for EOF.
292 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);
294 #include "cfg-mencoder.h"
296 #ifdef CONFIG_DVDREAD
297 #include "spudec.h"
298 #endif
299 #include "vobsub.h"
301 #include "libao2/audio_out.h"
302 /* FIXME */
303 static void mencoder_exit(int level, const char *how)
305 if (how)
306 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ExitingHow, how);
307 else
308 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_Exiting);
310 exit(level);
313 static void parse_cfgfiles( m_config_t* conf )
315 char *conffile;
316 if (!disable_system_conf &&
317 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mencoder.conf") < 0)
318 mencoder_exit(1,MSGTR_ConfigFileError);
320 if (!disable_user_conf) {
321 if ((conffile = get_path("mencoder.conf")) == NULL) {
322 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
323 } else {
324 if (m_config_parse_config_file(conf, conffile) < 0)
325 mencoder_exit(1,MSGTR_ConfigFileError);
326 free(conffile);
332 //---------------------------------------------------------------------------
334 static int dec_audio(sh_audio_t *sh_audio,unsigned char* buffer,int total){
335 int size=0;
336 int at_eof=0;
337 while(size<total && !at_eof){
338 int len=total-size;
339 if(len>MAX_OUTBURST) len=MAX_OUTBURST;
340 if (decode_audio(sh_audio, len) < 0) at_eof=1;
341 if(len>sh_audio->a_out_buffer_len) len=sh_audio->a_out_buffer_len;
342 fast_memcpy(buffer+size,sh_audio->a_out_buffer,len);
343 sh_audio->a_out_buffer_len-=len; size+=len;
344 if(sh_audio->a_out_buffer_len>0)
345 fast_memcpy(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[len],sh_audio->a_out_buffer_len);
347 return size;
350 //---------------------------------------------------------------------------
352 // this function returns the absoloute time for which MEncoder will switch files or move in the file.
353 // so audio can be cut correctly. -1 if there is no limit.
354 static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v);
356 static volatile int at_eof=0;
357 static volatile int interrupted=0;
359 static void exit_sighandler(int x){
360 at_eof=1;
361 interrupted=2; /* 1 means error */
364 static muxer_t* muxer=NULL;
366 extern void print_wave_header(WAVEFORMATEX *h, int verbose_level);
368 int main(int argc,char* argv[]){
370 stream_t* stream=NULL;
371 stream_t* ostream=NULL;
372 demuxer_t* demuxer=NULL;
373 stream_t* stream2=NULL;
374 demuxer_t* demuxer2=NULL;
375 demux_stream_t *d_audio=NULL;
376 demux_stream_t *d_video=NULL;
377 demux_stream_t *d_dvdsub=NULL;
378 sh_audio_t *sh_audio=NULL;
379 sh_video_t *sh_video=NULL;
380 int file_format=DEMUXER_TYPE_UNKNOWN;
381 int i=DEMUXER_TYPE_UNKNOWN;
382 void *vobsub_writer=NULL;
383 s_frame_data frame_data = { .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
385 uint32_t ptimer_start;
386 uint32_t audiorate=0;
387 uint32_t videorate=0;
388 uint32_t audiosamples=1;
389 uint32_t videosamples=1;
390 uint32_t skippedframes=0;
391 uint32_t duplicatedframes=0;
392 uint32_t badframes=0;
394 muxer_stream_t* mux_a=NULL;
395 muxer_stream_t* mux_v=NULL;
396 off_t muxer_f_size=0;
398 double v_pts_corr=0;
399 double v_timer_corr=0;
401 m_entry_t* filelist = NULL;
402 char* filename=NULL;
404 int decoded_frameno=0;
405 int next_frameno=-1;
406 int curfile=0;
407 int new_srate=0;
409 unsigned int timer_start=0;
410 ao_data_t ao_data = {0,0,0,0,OUTBURST,-1,0};
412 audio_encoding_params_t aparams;
413 audio_encoder_t *aencoder = NULL;
415 mp_msg_init();
417 // Create the config context and register the options
418 set_default_mencoder_options(&opts);
419 mconfig = m_config_new(&opts, cfg_include);
420 m_config_register_options(mconfig,mencoder_opts);
422 // Preparse the command line
423 m_config_preparse_command_line(mconfig,argc,argv);
425 mp_msg(MSGT_CPLAYER,MSGL_INFO, "MEncoder " VERSION " (C) 2000-2008 MPlayer Team\n");
427 /* Test for cpu capabilities (and corresponding OS support) for optimizing */
428 GetCpuCaps(&gCpuCaps);
429 #ifdef ARCH_X86
430 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: Type: %d MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
431 gCpuCaps.cpuType,gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
432 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
433 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
434 #ifdef RUNTIME_CPUDETECT
435 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
436 #else
437 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
438 #ifdef HAVE_MMX
439 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
440 #endif
441 #ifdef HAVE_MMX2
442 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
443 #endif
444 #ifdef HAVE_3DNOW
445 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
446 #endif
447 #ifdef HAVE_3DNOWEX
448 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
449 #endif
450 #ifdef HAVE_SSE
451 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
452 #endif
453 #ifdef HAVE_SSE2
454 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
455 #endif
456 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n\n");
457 #endif
458 #endif
460 #if defined(WIN32) && defined(CONFIG_WIN32DLL)
461 set_path_env();
462 #endif /*WIN32 && CONFIG_WIN32DLL*/
464 InitTimer();
466 // check codec.conf
467 if(!codecs_file || !parse_codec_cfg(codecs_file)){
468 if(!parse_codec_cfg(get_path("codecs.conf"))){
469 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
470 if(!parse_codec_cfg(NULL)){
471 mencoder_exit(1,NULL);
473 mp_msg(MSGT_MENCODER,MSGL_V,MSGTR_BuiltinCodecsConf);
478 parse_cfgfiles(mconfig);
479 filelist = m_config_parse_me_command_line(mconfig, argc, argv);
480 if(!filelist) mencoder_exit(1, MSGTR_ErrorParsingCommandLine);
483 char *extension;
485 if (!out_filename) mencoder_exit(1,MSGTR_MissingOutputFilename);
486 extension=strrchr(out_filename,'.');
487 if (extension != NULL && strlen(extension) > 3 && strlen(extension) < 6)
489 extension++;
491 switch (out_file_format)
493 case MUXER_TYPE_AVI:
494 if (strcasecmp(extension,"avi"))
495 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_MencoderWrongFormatAVI);
496 break;
498 case MUXER_TYPE_MPEG:
499 if (strcasecmp(extension,"mpg") &&
500 strcasecmp(extension,"mpeg") &&
501 strcasecmp(extension,"vob"))
502 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_MencoderWrongFormatMPG);
503 break;
507 /* Display what configure line was used */
508 mp_msg(MSGT_MENCODER, MSGL_V, "Configuration: " CONFIGURATION "\n");
511 if (frameno_filename) {
512 stream2=open_stream(frameno_filename,0,&i);
513 if(stream2){
514 demuxer2=demux_open(&opts, stream2,DEMUXER_TYPE_AVI,-1,-1,-2,NULL);
515 if(demuxer2) mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_UsingPass3ControlFile, frameno_filename);
516 else mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized);
520 #ifdef WIN32
521 if(proc_priority){
522 int i;
523 for(i=0; priority_presets_defs[i].name; i++){
524 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
525 break;
527 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_SettingProcessPriority,
528 priority_presets_defs[i].name);
529 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
531 #endif
533 // check font
534 #ifdef CONFIG_FREETYPE
535 init_freetype();
536 #endif
537 #ifdef CONFIG_FONTCONFIG
538 if(font_fontconfig <= 0)
540 #endif
541 #ifdef CONFIG_BITMAP_FONT
542 if(font_name){
543 vo_font=read_font_desc(font_name,font_factor,verbose>1);
544 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,font_name);
545 } else {
546 // try default:
547 vo_font=read_font_desc(get_path("font/font.desc"),font_factor,verbose>1);
548 if(!vo_font)
549 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
551 #endif
552 #ifdef CONFIG_FONTCONFIG
554 #endif
556 osd = osd_create();
558 /* HACK, for some weird reason, push() has to be called twice,
559 otherwise options are not saved correctly */
560 m_config_push(mconfig);
561 play_next_file:
562 m_config_push(mconfig);
563 m_entry_set_options(mconfig,&filelist[curfile]);
564 filename = filelist[curfile].name;
566 if(!filename){
567 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_MissingFilename);
568 mencoder_exit(1,NULL);
570 stream=open_stream(filename,0,&file_format);
572 if(!stream){
573 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_CannotOpenFile_Device);
574 mencoder_exit(1,NULL);
577 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_OpenedStream, file_format, (int)(stream->start_pos), (int)(stream->end_pos));
579 #ifdef CONFIG_DVDREAD
580 if(stream->type==STREAMTYPE_DVD){
581 if(audio_lang && opts.audio_id==-1) opts.audio_id=dvd_aid_from_lang(stream,audio_lang);
582 if(dvdsub_lang && opts.sub_id==-2) opts.sub_id=dvd_sid_from_lang(stream,dvdsub_lang);
584 #endif
586 #ifdef CONFIG_DVDNAV
587 if(stream->type==STREAMTYPE_DVDNAV){
588 if(audio_lang && opts.audio_id==-1) opts.audio_id=mp_dvdnav_aid_from_lang(stream,audio_lang);
589 if(dvdsub_lang && opts.sub_id==-2) opts.sub_id=mp_dvdnav_sid_from_lang(stream,dvdsub_lang);
591 #endif
593 stream->start_pos+=seek_to_byte;
595 if(stream_cache_size>0) stream_enable_cache(stream,stream_cache_size*1024,0,0);
597 if(demuxer2) opts.audio_id=-2; /* do NOT read audio packets... */
599 //demuxer=demux_open(stream,file_format,opts.video_id,opts.audio_id,opts.sub_id);
600 demuxer=demux_open(&opts, stream,file_format,opts.audio_id,opts.video_id,opts.sub_id,filename);
601 if(!demuxer){
602 mp_msg(MSGT_DEMUXER, MSGL_FATAL, MSGTR_FormatNotRecognized);
603 mp_msg(MSGT_DEMUXER, MSGL_FATAL, MSGTR_CannotOpenDemuxer);
604 mencoder_exit(1,NULL);
607 select_audio(demuxer, opts.audio_id, audio_lang);
609 if (opts.sub_id < 0 && dvdsub_lang)
610 opts.sub_id = demuxer_sub_track_by_lang(demuxer, dvdsub_lang);
612 if (opts.sub_id < 0)
613 opts.sub_id = demuxer_default_sub_track(demuxer);
615 for (i = 0; i < MAX_S_STREAMS; i++) {
616 sh_sub_t *sh = demuxer->s_streams[i];
617 if (sh && sh->sid == opts.sub_id) {
618 demuxer->sub->id = i;
619 demuxer->sub->sh = sh;
620 break;
624 if(dvd_chapter>1) {
625 float pts;
626 if (demuxer_seek_chapter(demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
627 seek_to_sec = pts;
630 d_audio=demuxer2 ? demuxer2->audio : demuxer->audio;
631 d_video=demuxer->video;
632 d_dvdsub=demuxer->sub;
633 sh_audio=d_audio->sh;
634 sh_video=d_video->sh;
636 if(!sh_video)
638 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_VideoStreamRequired);
639 mencoder_exit(1,NULL);
642 if(!video_read_properties(sh_video)){
643 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_CannotReadVideoProperties);
644 mencoder_exit(1,NULL);
647 mp_msg(MSGT_MENCODER,MSGL_INFO, MSGTR_FilefmtFourccSizeFpsFtime,
648 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
649 sh_video->fps,sh_video->frametime
652 if(force_fps){
653 sh_video->fps=force_fps;
654 sh_video->frametime=1.0f/sh_video->fps;
655 mp_msg(MSGT_MENCODER,MSGL_INFO,MSGTR_ForcingInputFPS, sh_video->fps);
658 if(sh_audio && out_audio_codec<0){
659 if(opts.audio_id==-2)
660 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_DemuxerDoesntSupportNosound);
661 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoAudioEncoderSelected);
662 mencoder_exit(1,NULL);
664 if(sh_video && out_video_codec<0){
665 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoVideoEncoderSelected);
666 mencoder_exit(1,NULL);
669 if(sh_audio && (out_audio_codec || seek_to_sec || !sh_audio->wf || opts.playback_speed != 1.0)){
670 // Go through the codec.conf and find the best codec...
671 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
672 if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
673 sh_audio=d_audio->sh=NULL; // failed to init :(
675 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
678 if (sh_audio) {
679 new_srate = sh_audio->samplerate;
680 if (opts.playback_speed != 1.0) {
681 new_srate *= opts.playback_speed;
682 // limits are taken from libaf/af_resample.c
683 if (new_srate < 8000) new_srate = 8000;
684 if (new_srate > 192000) new_srate = 192000;
685 opts.playback_speed = (float)new_srate / (float)sh_audio->samplerate;
689 // after reading video params we should load subtitles because
690 // we know fps so now we can adjust subtitles time to ~6 seconds AST
691 // check .sub
692 // current_module="read_subtitles_file";
693 if(sub_name && sub_name[0]){
694 subdata=sub_read_file(sub_name[0], sh_video->fps);
695 if(!subdata) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,sub_name[0]);
696 } else
697 if(sub_auto && filename) { // auto load sub file ...
698 char **tmp = NULL;
699 int i = 0;
700 char *psub = get_path( "sub/" );
701 tmp = sub_filenames((psub ? psub : ""), filename);
702 free(psub);
703 subdata=sub_read_file(tmp[0], sh_video->fps);
704 while (tmp[i])
705 free(tmp[i++]);
706 free(tmp);
709 // set up video encoder:
711 if (!curfile) { // curfile is non zero when a second file is opened
712 if (vobsub_out) {
713 unsigned int palette[16], width, height;
714 unsigned char tmp[3] = { 0, 0, 0 };
715 if (spudec_ifo && vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, opts.sub_id, tmp) >= 0)
716 vobsub_writer = vobsub_out_open(vobsub_out, palette, sh_video->disp_w, sh_video->disp_h,
717 vobsub_out_id?vobsub_out_id:(char *)tmp, vobsub_out_index);
718 #ifdef CONFIG_DVDREAD
719 if (vobsub_writer == NULL) {
720 char tmp[3];
721 if (vobsub_out_id == NULL && stream->type == STREAMTYPE_DVD) {
722 int i;
723 dvd_priv_t *dvd = (dvd_priv_t*)stream->priv;
724 for (i = 0; i < dvd->nr_of_subtitles; ++i)
725 if (dvd->subtitles[i].id == opts.sub_id) {
726 tmp[0] = (dvd->subtitles[i].language >> 8) & 0xff;
727 tmp[1] = dvd->subtitles[i].language & 0xff;
728 tmp[2] = 0;
729 vobsub_out_id = tmp;
730 break;
733 vobsub_writer=vobsub_out_open(vobsub_out, stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
734 sh_video->disp_w, sh_video->disp_h, vobsub_out_id, vobsub_out_index);
736 #endif
738 else {
739 if (spudec_ifo) {
740 unsigned int palette[16], width, height;
741 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
742 vo_spudec=spudec_new_scaled(palette, sh_video->disp_w, sh_video->disp_h);
744 #ifdef CONFIG_DVDREAD
745 if (vo_spudec==NULL) {
746 vo_spudec=spudec_new_scaled(stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
747 sh_video->disp_w, sh_video->disp_h);
749 #endif
752 // Apply current settings for forced subs
753 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
755 ostream = open_output_stream(out_filename, 0);
756 if(!ostream) {
757 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CannotOpenOutputFile, out_filename);
758 mencoder_exit(1,NULL);
761 muxer=muxer_new_muxer(out_file_format,ostream);
762 if(!muxer) {
763 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CannotInitializeMuxer);
764 mencoder_exit(1,NULL);
766 #if 0
767 //disabled: it horrybly distorts filtered sound
768 if(out_file_format == MUXER_TYPE_MPEG) audio_preload = 0;
769 #endif
771 muxer->audio_delay_fix = audio_delay_fix;
773 // ============= VIDEO ===============
775 mux_v=muxer_new_stream(muxer,MUXER_TYPE_VIDEO);
777 mux_v->buffer_size=0x200000; // 2MB
778 mux_v->buffer=malloc(mux_v->buffer_size);
780 mux_v->source=sh_video;
782 mux_v->h.dwSampleSize=0; // VBR
783 #ifdef CONFIG_LIBAVCODEC
785 double fps = force_ofps?force_ofps:sh_video->fps*opts.playback_speed;
786 AVRational q= av_d2q(fps, fps*1001+2);
787 mux_v->h.dwScale= q.den;
788 mux_v->h.dwRate = q.num;
790 #else
791 mux_v->h.dwScale=10000;
792 mux_v->h.dwRate=mux_v->h.dwScale*(force_ofps?force_ofps:sh_video->fps*opts.playback_speed);
793 #endif
795 mux_v->codec=out_video_codec;
797 mux_v->bih=NULL;
799 sh_video->codec=NULL;
800 sh_video->vfilter=NULL; // fixme!
802 switch(mux_v->codec){
803 case VCODEC_COPY:
804 if (!curfile) {
805 if (sh_video->bih) {
806 mux_v->bih=malloc(sh_video->bih->biSize);
807 memcpy(mux_v->bih, sh_video->bih, sh_video->bih->biSize);
809 else
811 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
812 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
813 mux_v->bih->biWidth=sh_video->disp_w;
814 mux_v->bih->biHeight=sh_video->disp_h;
815 mux_v->bih->biCompression=sh_video->format;
816 mux_v->bih->biPlanes=1;
817 mux_v->bih->biBitCount=24; // FIXME!!!
818 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
821 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
822 mux_v->bih->biWidth, mux_v->bih->biHeight,
823 mux_v->bih->biBitCount, mux_v->bih->biCompression);
825 if (curfile) {
826 if (sh_video->bih) {
827 if ((mux_v->bih->biSize != sh_video->bih->biSize) ||
828 memcmp(mux_v->bih, sh_video->bih, sh_video->bih->biSize))
830 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
831 sh_video->bih->biWidth, sh_video->bih->biHeight,
832 sh_video->bih->biBitCount, sh_video->bih->biCompression);
833 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_FrameCopyFileMismatch);
834 mencoder_exit(1,NULL);
837 else {
838 if ((mux_v->bih->biWidth != sh_video->disp_w) ||
839 (mux_v->bih->biHeight != sh_video->disp_h) ||
840 (mux_v->bih->biCompression != sh_video->format)) {
841 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
842 sh_video->disp_w, sh_video->disp_w, 24, sh_video->format);
843 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_FrameCopyFileMismatch);
844 mencoder_exit(1,NULL);
848 break;
849 case VCODEC_FRAMENO:
850 if (!curfile) {
851 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
852 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
853 mux_v->bih->biWidth=sh_video->disp_w;
854 mux_v->bih->biHeight=sh_video->disp_h;
855 mux_v->bih->biPlanes=1;
856 mux_v->bih->biBitCount=24;
857 mux_v->bih->biCompression=mmioFOURCC('F','r','N','o');
858 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
860 break;
861 default: {
862 static vf_instance_t * ve = NULL;
863 if (!ve) {
864 switch(mux_v->codec){
865 case VCODEC_LIBAVCODEC:
866 sh_video->vfilter=vf_open_encoder(&opts, NULL,"lavc",(char *)mux_v); break;
867 case VCODEC_RAW:
868 sh_video->vfilter=vf_open_encoder(&opts, NULL,"raw",(char *)mux_v); break;
869 case VCODEC_VFW:
870 sh_video->vfilter=vf_open_encoder(&opts, NULL,"vfw",(char *)mux_v); break;
871 case VCODEC_LIBDV:
872 sh_video->vfilter=vf_open_encoder(&opts, NULL,"libdv",(char *)mux_v); break;
873 case VCODEC_XVID:
874 sh_video->vfilter=vf_open_encoder(&opts, NULL,"xvid",(char *)mux_v); break;
875 case VCODEC_QTVIDEO:
876 sh_video->vfilter=vf_open_encoder(&opts, NULL,"qtvideo",(char *)mux_v); break;
877 case VCODEC_NUV:
878 sh_video->vfilter=vf_open_encoder(&opts, NULL,"nuv",(char *)mux_v); break;
879 case VCODEC_X264:
880 sh_video->vfilter=vf_open_encoder(&opts, NULL,"x264",(char *)mux_v); break;
882 if(!mux_v->bih || !sh_video->vfilter){
883 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_EncoderOpenFailed);
884 mencoder_exit(1,NULL);
886 ve = sh_video->vfilter;
887 } else sh_video->vfilter = ve;
888 // append 'expand' filter, it fixes stride problems and renders osd:
889 if (auto_expand) {
890 char* vf_args[] = { "osd", "1", NULL };
891 sh_video->vfilter=vf_open_filter(&opts, sh_video->vfilter,"expand",vf_args);
893 sh_video->vfilter=append_filters(sh_video->vfilter, opts.vf_settings);
895 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
896 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
897 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
898 if(!sh_video->initialized) mencoder_exit(1,NULL);
902 if (!curfile) {
903 /* force output fourcc to .. */
904 if ((force_fourcc != NULL) && (strlen(force_fourcc) >= 4))
906 mux_v->bih->biCompression = mmioFOURCC(force_fourcc[0], force_fourcc[1],
907 force_fourcc[2], force_fourcc[3]);
908 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ForcingOutputFourcc,
909 mux_v->bih->biCompression, (char *)&mux_v->bih->biCompression);
912 if (! ignore_start)
913 muxer->audio_delay_fix -= sh_video->stream_delay;
915 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
917 // ============= AUDIO ===============
918 if(sh_audio){
920 if (force_audiofmttag != -1) {
921 sh_audio->format = force_audiofmttag;
922 if (sh_audio->wf) {
923 sh_audio->wf->wFormatTag = sh_audio->format;
925 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ForcingOutputAudiofmtTag,
926 force_audiofmttag);
929 mux_a=muxer_new_stream(muxer,MUXER_TYPE_AUDIO);
931 mux_a->buffer_size=0x100000; //16384;
932 mux_a->buffer=malloc(mux_a->buffer_size);
933 if (!mux_a->buffer)
934 mencoder_exit(1,MSGTR_MemAllocFailed);
936 mux_a->source=sh_audio;
938 mux_a->codec=out_audio_codec;
940 ao_data.samplerate = force_srate;
941 ao_data.channels = 0;
942 ao_data.format = audio_output_format;
943 if(!init_audio_filters(sh_audio,
944 // input:
945 new_srate,
946 // output:
947 &ao_data.samplerate, &ao_data.channels, &ao_data.format)) {
948 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
949 mencoder_exit(1, NULL);
952 aparams.channels = ao_data.channels;
953 aparams.sample_rate = ao_data.samplerate;
954 aparams.audio_preload = 1000 * audio_preload;
955 if(mux_a->codec != ACODEC_COPY) {
956 aencoder = new_audio_encoder(mux_a, &aparams);
957 if(!aencoder)
958 mencoder_exit(1, NULL);
959 if(!init_audio_filters(sh_audio,
960 new_srate,
961 &aparams.sample_rate, &aparams.channels, &aencoder->input_format)) {
962 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_NoMatchingFilter);
963 mencoder_exit(1,NULL);
966 switch(mux_a->codec){
967 case ACODEC_COPY:
968 if (opts.playback_speed != 1.0) mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_NoSpeedWithFrameCopy);
969 if (sh_audio->format >= 0x10000) {
970 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantCopyAudioFormat, sh_audio->format);
971 mencoder_exit(1,NULL);
973 if (sh_audio->wf){
974 mux_a->wf=malloc(sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
975 memcpy(mux_a->wf, sh_audio->wf, sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
976 if(!sh_audio->i_bps) sh_audio->i_bps=mux_a->wf->nAvgBytesPerSec;
977 } else {
978 mux_a->wf = malloc(sizeof(WAVEFORMATEX));
979 mux_a->wf->nBlockAlign = 1; //mux_a->h.dwSampleSize;
980 mux_a->wf->wFormatTag = sh_audio->format;
981 mux_a->wf->nChannels = sh_audio->channels;
982 mux_a->wf->nSamplesPerSec = sh_audio->samplerate;
983 mux_a->wf->nAvgBytesPerSec=sh_audio->i_bps; //mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
984 mux_a->wf->wBitsPerSample = 16; // FIXME
985 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
987 if(sh_audio->audio.dwScale){
988 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize;
989 mux_a->h.dwScale=sh_audio->audio.dwScale;
990 mux_a->h.dwRate=sh_audio->audio.dwRate;
991 } else {
992 mux_a->h.dwSampleSize=mux_a->wf->nBlockAlign;
993 mux_a->h.dwScale=mux_a->h.dwSampleSize;
994 mux_a->h.dwRate=mux_a->wf->nAvgBytesPerSec;
996 mux_a->h.dwRate *= opts.playback_speed;
997 mux_a->wf->nSamplesPerSec *= opts.playback_speed;
998 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
999 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
1000 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
1001 break;
1004 if ( mp_msg_test(MSGT_MENCODER,MSGL_DBG2) ) print_wave_header(mux_a->wf, MSGL_DBG2);
1006 if (! ignore_start)
1007 muxer->audio_delay_fix += sh_audio->stream_delay;
1009 } // if(sh_audio)
1011 decoded_frameno=0;
1013 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
1014 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
1015 signal(SIGTERM,exit_sighandler); // kill
1016 signal(SIGHUP,exit_sighandler); // broken terminal line
1017 signal(SIGPIPE,exit_sighandler); // broken pipe
1019 timer_start=GetTimerMS();
1020 } // if (!curfile) // if this was the first file.
1021 else {
1022 if (!mux_a != !sh_audio) {
1023 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoAudioFileMismatch);
1024 mencoder_exit(1,NULL);
1026 if (sh_audio && mux_a->codec == ACODEC_COPY) {
1027 if (opts.playback_speed != 1.0) mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_NoSpeedWithFrameCopy);
1028 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
1029 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
1030 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
1031 if (sh_audio->wf) {
1032 if ((mux_a->wf->wFormatTag != sh_audio->wf->wFormatTag) ||
1033 (mux_a->wf->nChannels != sh_audio->wf->nChannels) ||
1034 (mux_a->wf->nSamplesPerSec != sh_audio->wf->nSamplesPerSec * opts.playback_speed))
1036 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
1037 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * opts.playback_speed),
1038 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1039 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_AudioCopyFileMismatch);
1040 mencoder_exit(1,NULL);
1042 } else {
1043 if ((mux_a->wf->wFormatTag != sh_audio->format) ||
1044 (mux_a->wf->nChannels != sh_audio->channels) ||
1045 (mux_a->wf->nSamplesPerSec != sh_audio->samplerate * opts.playback_speed))
1047 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
1048 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * opts.playback_speed),
1049 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1050 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_AudioCopyFileMismatch);
1051 mencoder_exit(1,NULL);
1054 } else if (sh_audio) {
1055 int out_srate = mux_a->wf->nSamplesPerSec;
1056 int out_channels = mux_a->wf->nChannels;
1057 int out_format = aencoder->input_format;
1058 if (!init_audio_filters(sh_audio, new_srate,
1059 &out_srate, &out_channels,
1060 &out_format)) {
1061 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoMatchingFilter);
1062 mencoder_exit(1, NULL);
1064 mux_a->wf->nSamplesPerSec = out_srate;
1065 mux_a->wf->nChannels = out_channels;
1069 if (seek_to_sec) {
1070 demux_seek(demuxer, seek_to_sec, audio_delay, 1);
1071 // there is 2 way to handle the -ss option in 3-pass mode:
1072 // > 1. do the first pass for the whole file, and use -ss for 2nd/3rd pases only
1073 // > 2. do all the 3 passes with the same -ss value
1074 // this line enables behaviour 1. (and kills 2. at the same time):
1075 // if(demuxer2) demux_seek(demuxer2, d, audio_delay, 1);
1078 if (out_file_format == MUXER_TYPE_MPEG)
1080 if (audio_preload > 0.4) {
1081 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_LimitingAudioPreload);
1082 audio_preload = 0.4;
1084 if (audio_density < 4) {
1085 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_IncreasingAudioDensity);
1086 audio_density = 4;
1090 if(file_format == DEMUXER_TYPE_TV)
1092 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_ZeroingAudioPreloadAndMaxPtsCorrection);
1093 audio_preload = 0.0;
1094 default_max_pts_correction = 0;
1097 play_n_frames=play_n_frames_mf;
1098 if (curfile && end_at.type == END_AT_TIME) end_at.pos += mux_v->timer;
1100 if (edl_records) free_edl(edl_records);
1101 next_edl_record = edl_records = NULL;
1102 edl_muted = 0;
1103 edl_seeking = 1;
1104 if (edl_filename) {
1105 next_edl_record = edl_records = edl_parse_file();
1108 if (sh_audio && audio_delay != 0.) fixdelay(d_video, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1110 while(!at_eof){
1112 int blit_frame=0;
1113 float a_pts=0;
1114 float v_pts=0;
1115 int skip_flag=0; // 1=skip -1=duplicate
1117 if((end_at.type == END_AT_SIZE && end_at.pos <= stream_tell(muxer->stream)) ||
1118 (end_at.type == END_AT_TIME && end_at.pos < mux_v->timer))
1119 break;
1121 if(play_n_frames>=0){
1122 --play_n_frames;
1123 if(play_n_frames<0) break;
1125 if(dvd_last_chapter>0) {
1126 int cur_chapter = demuxer_get_current_chapter(demuxer);
1127 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
1128 break;
1131 goto_redo_edl:
1132 if (next_edl_record && sh_video && sh_video->pts >= next_edl_record->start_sec) {
1133 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1134 float last_pos = d_video->pts;
1135 int result;
1136 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop [%f], length [%f]\n",
1137 next_edl_record->start_sec, next_edl_record->stop_sec, next_edl_record->length_sec);
1139 result = edl_seek(next_edl_record, demuxer, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1141 if (result == 2) { at_eof=1; break; } // EOF
1142 else if (result == 0) edl_seeking = 0; // no seeking
1143 else { // sucess
1144 edl_muted = 0;
1145 if (last_pos >= sh_video->pts) {
1146 // backwards seek detected!! Forget about this EDL skip altogether.
1147 next_edl_record = next_edl_record->next;
1149 else for (next_edl_record = edl_records; next_edl_record; next_edl_record = next_edl_record->next) {
1150 /* note the use of stop_sec,
1151 meaning if by some magical way we landed in the MIDDLE of a censored area,
1152 in the next loop it will jump out of it.
1154 if (next_edl_record->stop_sec > sh_video->pts) break; // we got to the right place.
1155 if (next_edl_record->action == EDL_MUTE) edl_muted = !edl_muted; // toggle mute each time.
1158 /* for a pedantic EDL, that doesn't show even a single
1159 frame from the "censored" area, uncomment next line. */
1160 goto goto_redo_edl;
1162 } else if (next_edl_record->action == EDL_MUTE) {
1163 edl_muted = !edl_muted; // This variable does nothing for now.
1164 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n", next_edl_record->start_sec );
1165 next_edl_record=next_edl_record->next;
1170 if(sh_audio){
1171 // get audio:
1172 while(mux_a->timer-audio_preload<mux_v->timer){
1173 float tottime;
1174 int len=0;
1176 ptimer_start = GetTimerMS();
1177 // CBR - copy 0.5 sec of audio
1178 // or until the end of video:
1179 tottime = stop_time(demuxer, mux_v);
1180 if (tottime != -1) {
1181 tottime -= mux_a->timer;
1182 if (tottime > 1./audio_density) tottime = 1./audio_density;
1184 else tottime = 1./audio_density;
1186 // let's not output more audio than necessary
1187 if (tottime <= 0) break;
1189 if(aencoder)
1191 if(mux_a->h.dwSampleSize) /* CBR */
1193 if(aencoder->set_decoded_len)
1195 len = mux_a->h.dwSampleSize*(int)(mux_a->h.dwRate*tottime);
1196 aencoder->set_decoded_len(aencoder, len);
1198 else
1199 len = aencoder->decode_buffer_size;
1201 len = dec_audio(sh_audio, aencoder->decode_buffer, len);
1202 mux_a->buffer_len += aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len,
1203 aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1204 if(mux_a->buffer_len < mux_a->wf->nBlockAlign)
1205 len = 0;
1206 else
1207 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1209 else /* VBR */
1211 int sz = 0;
1212 while(1)
1214 len = 0;
1215 if(! sz)
1216 sz = aencoder->get_frame_size(aencoder);
1217 if(sz > 0 && mux_a->buffer_len >= sz)
1219 len = sz;
1220 break;
1222 len = dec_audio(sh_audio,aencoder->decode_buffer, aencoder->decode_buffer_size);
1223 if(len <= 0)
1225 len = 0;
1226 break;
1228 len = aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len, aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1229 mux_a->buffer_len += len;
1232 if (mux_v->timer == 0) mux_a->h.dwInitialFrames++;
1234 else {
1235 if(mux_a->h.dwSampleSize){
1236 switch(mux_a->codec){
1237 case ACODEC_COPY: // copy
1238 len=mux_a->wf->nAvgBytesPerSec*tottime;
1239 len/=mux_a->h.dwSampleSize;if(len<1) len=1;
1240 len*=mux_a->h.dwSampleSize;
1241 len=demux_read_data(sh_audio->ds,mux_a->buffer,len);
1242 break;
1244 } else {
1245 // VBR - encode/copy an audio frame
1246 switch(mux_a->codec){
1247 case ACODEC_COPY: // copy
1248 len=ds_get_packet(sh_audio->ds,(unsigned char**) &mux_a->buffer);
1249 break;
1253 if(len<=0) break; // EOF?
1254 muxer_write_chunk(mux_a,len,0x10, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1255 if(!mux_a->h.dwSampleSize && mux_a->timer>0)
1256 mux_a->wf->nAvgBytesPerSec=0.5f+(double)mux_a->size/mux_a->timer; // avg bps (VBR)
1257 if(mux_a->buffer_len>=len){
1258 mux_a->buffer_len-=len;
1259 fast_memcpy(mux_a->buffer,mux_a->buffer+len,mux_a->buffer_len);
1263 audiosamples++;
1264 audiorate+= (GetTimerMS() - ptimer_start);
1269 // get video frame!
1271 if (!frame_data.already_read) {
1272 frame_data.in_size=video_read_frame(sh_video,&frame_data.frame_time,&frame_data.start,force_fps);
1273 sh_video->timer+=frame_data.frame_time;
1275 frame_data.frame_time /= opts.playback_speed;
1276 if(frame_data.in_size<0){ at_eof=1; break; }
1277 ++decoded_frameno;
1279 v_timer_corr-=frame_data.frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate;
1281 if(demuxer2){ // 3-pass encoding, read control file (frameno.avi)
1282 // find our frame:
1283 while(next_frameno<decoded_frameno){
1284 int* start;
1285 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1286 if(len<0){ at_eof=1;break;}
1287 if(len==0) --skip_flag; else // duplicate
1288 if(len==4) next_frameno=start[0];
1290 if(at_eof) break;
1291 // if(skip_flag) printf("!!!!!!!!!!!!\n");
1292 skip_flag=next_frameno-decoded_frameno;
1293 // find next frame:
1294 while(next_frameno<=decoded_frameno){
1295 int* start;
1296 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1297 if(len<0){ at_eof=1;break;}
1298 if(len==0) --skip_flag; else // duplicate
1299 if(len==4) next_frameno=start[0];
1301 // if(at_eof) break;
1302 // printf("Current fno=%d requested=%d skip=%d \n",decoded_frameno,fno,skip_flag);
1303 } else {
1305 // check frame duplicate/drop:
1307 //printf("\r### %5.3f ###\n",v_timer_corr);
1308 float mux_frametime = (float)mux_v->h.dwScale/mux_v->h.dwRate;
1310 if (v_timer_corr >= mux_frametime && (skip_limit<0 || skip_flag < skip_limit)) {
1311 v_timer_corr-=mux_frametime;
1312 ++skip_flag; // skip
1314 while (v_timer_corr <= -mux_frametime && (skip_limit<0 || -skip_flag < skip_limit)) {
1315 v_timer_corr+=mux_frametime;
1316 --skip_flag; // dup
1319 // either v_pts_corr is big, more than 2 times framerate, then we follow its advice,
1320 // or, it cancels out v_timer_corr, in which case be happy and do nothing.
1322 while ((v_pts_corr <= -mux_frametime && skip_flag > 0) || (v_pts_corr <= -2*mux_frametime)) {
1323 v_pts_corr+=mux_frametime;
1324 --skip_flag; // dup
1326 if ((v_pts_corr >= mux_frametime && skip_flag < 0) || (v_pts_corr >= 2*mux_frametime)) {
1327 if (skip_flag<=0) { // we can't skip more than 1 frame now
1328 v_pts_corr-=mux_frametime;
1329 ++skip_flag; // skip
1333 } // demuxer2
1335 ptimer_start = GetTimerMS();
1337 switch(mux_v->codec){
1338 case VCODEC_COPY:
1339 mux_v->buffer=frame_data.start;
1340 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);
1341 break;
1342 case VCODEC_FRAMENO:
1343 mux_v->buffer=(unsigned char *)&decoded_frameno; // tricky
1344 if(skip_flag<=0) muxer_write_chunk(mux_v,sizeof(int),0x10, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1345 break;
1346 default:
1347 // decode_video will callback down to ve_*.c encoders, through the video filters
1348 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ, osd);
1349 {void *decoded_frame = decode_video(sh_video,frame_data.start,frame_data.in_size,
1350 skip_flag>0 && (!sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) != CONTROL_TRUE), MP_NOPTS_VALUE);
1351 blit_frame = decoded_frame && filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE, osd);}
1353 if (sh_video->vf_initialized < 0) mencoder_exit(1, NULL);
1355 if(!blit_frame){
1356 if (play_n_frames >= 0)
1357 play_n_frames++;
1358 badframes++;
1359 if(skip_flag<=0){
1360 // unwanted skipping of a frame, what to do?
1361 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1362 #if 0
1363 // Old code apparently made under the assumption that !blit_frame means
1364 // decoding failed due to corruption or something.. but duplication and
1365 // skipping of frames should be entirely disabled when skip_limit==0,
1366 // and must be in order for many filters to work with -noskip.
1367 // Eventually this entire block should probably be removed.
1368 if(skip_limit==0){
1369 // skipping not allowed -> write empty frame:
1370 if (!encode_duplicates || !sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1371 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1372 } else {
1373 // skipping allowed -> skip it and distriubute timer error:
1374 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1376 #endif
1381 videosamples++;
1382 videorate+=(GetTimerMS() - ptimer_start);
1384 if(skip_flag<0){
1385 // duplicate frame
1386 if(!quiet) mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_DuplicateFrames,-skip_flag);
1387 while(skip_flag<0){
1388 duplicatedframes++;
1389 if (!encode_duplicates || !sh_video->vfilter || sh_video->vfilter->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1390 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1391 ++skip_flag;
1393 } else
1394 if(skip_flag>0){
1395 // skip frame
1396 if(!quiet) mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_SkipFrame);
1397 skippedframes++;
1398 --skip_flag;
1401 if(sh_audio && !demuxer2){
1402 float AV_delay,x;
1403 // A-V sync!
1404 #if 0
1405 if(pts_from_bps){
1406 unsigned int samples=(sh_audio->audio.dwSampleSize)?
1407 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
1408 (d_audio->block_no); // <- used for VBR audio
1409 // printf("samples=%d \n",samples);
1410 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
1411 delay_corrected=1;
1412 } else
1413 #endif
1415 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
1416 a_pts=d_audio->pts;
1417 if(!delay_corrected) if(a_pts) delay_corrected=1;
1418 //printf("*** %5.3f ***\n",a_pts);
1419 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1421 v_pts=sh_video ? sh_video->pts : d_video->pts;
1422 // av = compensated (with out buffering delay) A-V diff
1423 AV_delay=(a_pts-v_pts);
1424 AV_delay-=audio_delay;
1425 AV_delay /= opts.playback_speed;
1426 AV_delay-=mux_a->timer-(mux_v->timer-(v_timer_corr+v_pts_corr));
1427 // adjust for encoder delays
1428 AV_delay -= (float) mux_a->encoder_delay * mux_a->h.dwScale/mux_a->h.dwRate;
1429 AV_delay += (float) mux_v->encoder_delay * mux_v->h.dwScale/mux_v->h.dwRate;
1430 // compensate input video timer by av:
1431 x=AV_delay*0.1f;
1432 if(x<-max_pts_correction) x=-max_pts_correction; else
1433 if(x> max_pts_correction) x= max_pts_correction;
1434 if(default_max_pts_correction>=0)
1435 max_pts_correction=default_max_pts_correction*opts.playback_speed;
1436 else
1437 max_pts_correction=sh_video->frametime*0.10 *opts.playback_speed; // +-10% of time
1438 // sh_video->timer-=x;
1439 c_total+=x;
1440 v_pts_corr+=x;
1443 // printf("A:%6.1f V:%6.1f A-V:%7.3f oAV:%7.3f diff:%7.3f ct:%7.3f vpc:%7.3f \r",
1444 // a_pts,v_pts,a_pts-v_pts,
1445 // (float)(mux_a->timer-mux_v->timer),
1446 // AV_delay, c_total, v_pts_corr );
1447 // printf("V:%6.1f \r", d_video->pts );
1449 #if 0
1450 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",
1451 a_pts,v_pts,a_pts-v_pts,c_total,
1452 (int)sh_video->num_frames,(int)sh_video->num_frames_decoded,
1453 (sh_video->timer>0.5)?(int)(100.0*video_time_usage/(double)sh_video->timer):0,
1454 (sh_video->timer>0.5)?(int)(100.0*vout_time_usage/(double)sh_video->timer):0,
1455 (sh_video->timer>0.5)?(100.0*audio_time_usage/(double)sh_video->timer):0
1456 ,cache_fill_status
1458 #endif
1460 { float t=(GetTimerMS()-timer_start)*0.001f;
1461 float len=(demuxer->movi_end-demuxer->movi_start);
1462 float p=len>1000 ? (float)(demuxer->filepos-demuxer->movi_start) / len :
1463 (demuxer_get_percent_pos(demuxer) / 100.0);
1464 #if 0
1465 if(!len && sh_audio && sh_audio->audio.dwLength>100){
1466 p=(sh_audio->audio.dwSampleSize? ds_tell(sh_audio->ds)/sh_audio->audio.dwSampleSize : sh_audio->ds->block_no)
1467 / (float)(sh_audio->audio.dwLength);
1469 #endif
1470 #if 0
1471 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"%d < %d < %d \r",
1472 (int)demuxer->movi_start,
1473 (int)demuxer->filepos,
1474 (int)demuxer->movi_end);
1475 #else
1476 if(!quiet) {
1477 if( mp_msg_test(MSGT_STATUSLINE,MSGL_V) ) {
1478 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",
1479 mux_v->timer, decoded_frameno, (int)(p*100),
1480 (t>1) ? (int)(decoded_frameno/t+0.5) : 0,
1481 (p>0.001) ? (int)((t/p-t)/60) : 0,
1482 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1483 v_pts_corr,
1484 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1485 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0,
1486 audiorate/audiosamples, videorate/videosamples,
1487 duplicatedframes, badframes, skippedframes
1489 } else
1490 mp_msg(MSGT_STATUSLINE,MSGL_STATUS,"Pos:%6.1fs %6df (%2d%%) %5.2ffps Trem:%4dmin %3dmb A-V:%5.3f [%d:%d]\r",
1491 mux_v->timer, decoded_frameno, (int)(p*100),
1492 (t>1) ? (float)(decoded_frameno/t) : 0,
1493 (p>0.001) ? (int)((t/p-t)/60) : 0,
1494 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1495 v_pts_corr,
1496 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1497 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0
1500 #endif
1502 fflush(stdout);
1504 #ifdef CONFIG_DVDREAD
1505 // DVD sub:
1506 if(vobsub_writer){
1507 unsigned char* packet=NULL;
1508 int len;
1509 while((len=ds_get_packet_sub(d_dvdsub,&packet))>0){
1510 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);
1511 vobsub_out_output(vobsub_writer,packet,len,mux_v->timer + d_dvdsub->pts - sh_video->pts);
1514 else
1515 #endif
1516 update_subtitles(sh_video, d_dvdsub, 0);
1518 frame_data = (s_frame_data){ .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
1520 #if 0
1521 if(ferror(muxer_f)) {
1522 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile, out_filename);
1523 mencoder_exit(1, NULL);
1525 #endif
1527 } // while(!at_eof)
1529 if (!interrupted && filelist[++curfile].name != 0) {
1530 if (sh_video && sh_video->vfilter) { // Before uniniting sh_video and the filter chain, break apart the VE.
1531 vf_instance_t * ve; // this will be the filter right before the ve.
1532 for (ve = sh_video->vfilter; ve->next && ve->next->next; ve = ve->next);
1534 if (ve->next) ve->next = NULL; // I'm telling the last filter, before the VE, there is nothing after it
1535 else sh_video->vfilter = NULL; // There is no chain except the VE.
1538 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1539 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1540 if(demuxer) free_demuxer(demuxer);
1541 if(stream) free_stream(stream); // kill cache thread
1543 at_eof = 0;
1545 m_config_pop(mconfig);
1546 goto play_next_file;
1549 /* Emit the remaining frames in the video system */
1550 /*TODO emit frmaes delayed by decoder lag*/
1551 if(sh_video && sh_video->vfilter){
1552 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_FlushingVideoFrames);
1553 if (!sh_video->vfilter->fmt.have_configured)
1554 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_FiltersHaveNotBeenConfiguredEmptyFile);
1555 else
1556 sh_video->vfilter->control(sh_video->vfilter,
1557 VFCTRL_FLUSH_FRAMES, 0);
1560 if(aencoder)
1561 if(aencoder->fixup)
1562 aencoder->fixup(aencoder);
1564 if (muxer->cont_write_index) muxer_write_index(muxer);
1565 muxer_f_size=stream_tell(muxer->stream);
1566 stream_seek(muxer->stream,0);
1567 if (muxer->cont_write_header) muxer_write_header(muxer); // update header
1568 #if 0
1569 if(ferror(muxer_f) || fclose(muxer_f) != 0) {
1570 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile, out_filename);
1571 mencoder_exit(1, NULL);
1573 #endif
1574 if(vobsub_writer)
1575 vobsub_out_close(vobsub_writer);
1577 if(out_video_codec==VCODEC_FRAMENO && mux_v->timer>100){
1578 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"650MB",(int)((650*1024*1024-muxer_f_size)/mux_v->timer/125));
1579 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"700MB",(int)((700*1024*1024-muxer_f_size)/mux_v->timer/125));
1580 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"800MB",(int)((800*1024*1024-muxer_f_size)/mux_v->timer/125));
1581 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"2 x 650MB",(int)((2*650*1024*1024-muxer_f_size)/mux_v->timer/125));
1582 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"2 x 700MB",(int)((2*700*1024*1024-muxer_f_size)/mux_v->timer/125));
1583 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"2 x 800MB",(int)((2*800*1024*1024-muxer_f_size)/mux_v->timer/125));
1586 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VideoStreamResult,
1587 (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);
1588 if(sh_audio)
1589 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_AudioStreamResult,
1590 (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);
1592 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1593 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1594 if(demuxer) free_demuxer(demuxer);
1595 if(stream) free_stream(stream); // kill cache thread
1597 return interrupted;
1601 #if 0
1602 /* Flip the image in src and store the result in dst. src and dst may overlap.
1603 width is the size of each line in bytes. */
1604 static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width,
1605 int height)
1607 uint8_t* tmp = malloc(width);
1608 int i;
1610 for(i = 0; i < height/2; i++) {
1611 fast_memcpy(tmp, &src[i*width], width);
1612 fast_memcpy(&dst[i * width], &src[(height - i) * width], width);
1613 fast_memcpy(&dst[(height - i) * width], tmp, width);
1616 free(tmp);
1617 return dst;
1619 #endif
1622 static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v) {
1623 float timeleft = -1;
1624 if (play_n_frames >= 0) timeleft = mux_v->timer + play_n_frames * (double)(mux_v->h.dwScale) / mux_v->h.dwRate;
1625 if (end_at.type == END_AT_TIME && (timeleft > end_at.pos || timeleft == -1)) timeleft = end_at.pos;
1626 if (next_edl_record && demuxer && demuxer->video) { // everything is OK to be checked
1627 float tmp = mux_v->timer + next_edl_record->start_sec - demuxer->video->pts;
1628 if (timeleft == -1 || timeleft > tmp) {
1629 // There's less time in EDL than what we already know
1630 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1631 timeleft = tmp;
1632 } else if (next_edl_record->action == EDL_MUTE) {
1633 //timeleft = next_edl_record->start_sec - demuxer->video->pts;
1634 // For the moment (and probably forever) EDL mute doesn't work in MEncoder
1638 return timeleft;
1641 static float calc_a_pts(demux_stream_t *d_audio) {
1642 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
1643 float a_pts = 0.;
1644 if (sh_audio)
1645 a_pts = d_audio->pts + (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1646 return a_pts;
1649 static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a) {
1650 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
1651 int samplesize, avg;
1652 float a_pts = calc_a_pts(d_audio);
1654 if (!sh_audio) return a_pts;
1656 if (sh_audio->audio.dwScale) samplesize = sh_audio->audio.dwSampleSize;
1657 else samplesize = (sh_audio->wf ? sh_audio->wf->nBlockAlign : 1);
1658 avg = (sh_audio->wf ? sh_audio->wf->nAvgBytesPerSec : sh_audio->i_bps);
1660 // after a demux_seek, a_pts will be zero until you read some audio.
1661 // carefully checking if a_pts is truely correct by reading tiniest amount of data possible.
1662 if (pts > a_pts && a_pts == 0.0 && samplesize) {
1663 if (demux_read_data(sh_audio->ds,mux_a->buffer,samplesize) <= 0) return a_pts; // EOF
1664 a_pts = calc_a_pts(d_audio);
1667 while (pts > a_pts) {
1668 int len;
1669 if (samplesize) {
1670 len = avg * (pts - a_pts > 0.5 ? 0.5 : pts - a_pts);
1671 len/= samplesize; if(len<1) len=1;
1672 len*= samplesize;
1673 len = demux_read_data(sh_audio->ds,mux_a->buffer,len);
1674 } else {
1675 unsigned char * crap;
1676 len = ds_get_packet(sh_audio->ds, &crap);
1678 if (len <= 0) break; // EOF of audio.
1679 a_pts = calc_a_pts(d_audio);
1681 return a_pts;
1684 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) {
1685 sh_video_t * sh_video = d_video->sh;
1686 vf_instance_t * vfilter = sh_video ? sh_video->vfilter : NULL;
1687 int done = 0;
1689 while (!interrupted) {
1690 float a_pts = 0.;
1692 if (!frame_data->already_read) { // when called after fixdelay, a frame is already read
1693 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
1694 if(frame_data->in_size<0) return 2;
1695 sh_video->timer += frame_data->frame_time;
1697 frame_data->already_read = 0;
1699 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
1701 if (done) {
1702 // wait for keyframe in case of -ovc copy
1703 if (!framecopy || (sh_video->ds->flags & 1)) {
1704 frame_data->already_read = 1;
1705 return 1;
1708 if (sh_video->pts >= end_pts) done = 1;
1710 if (vfilter) {
1711 sh_video->vfilter->control(sh_video->vfilter, VFCTRL_SET_OSD_OBJ,
1712 osd);
1713 int softskip = (vfilter->control(vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) == CONTROL_TRUE);
1714 void *decoded_frame = decode_video(sh_video, frame_data->start, frame_data->in_size, !softskip, MP_NOPTS_VALUE);
1715 if (decoded_frame)
1716 filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE, osd);
1719 if (print_info) mp_msg(MSGT_MENCODER, MSGL_STATUS,
1720 MSGTR_EdlSkipStartEndCurrent,
1721 next_edl_record->start_sec, next_edl_record->stop_sec,
1722 sh_video->pts, a_pts);
1724 if (interrupted) return 2;
1725 return 1;
1728 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) {
1729 // TODO: Find a way to encode silence instead of deleting video
1730 sh_video_t * sh_video = d_video->sh;
1731 float a_pts;
1733 // demux_seek has a weirdness that sh_video->pts is meaningless,
1734 // until a single frame is read... Same for audio actually too.
1735 // Reading one frame, and keeping it.
1736 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
1737 if(frame_data->in_size<0) return;
1738 sh_video->timer += frame_data->frame_time;
1739 frame_data->already_read = 1;
1741 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
1743 if (audio_delay > 0) return;
1744 else if (sh_video->pts - frame_data->frame_time + audio_delay >= a_pts) return;
1746 slowseek(a_pts - audio_delay, d_video, d_audio, mux_a, frame_data, framecopy, 0);
1749 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) {
1750 sh_video_t * sh_video = demuxer->video ? demuxer->video->sh : NULL;
1752 if (!sh_video) return 0;
1753 if (sh_video->pts >= next_edl_record->stop_sec) return 1; // nothing to do...
1755 if (!edl_seek_type) {
1756 if(demux_seek(demuxer, next_edl_record->stop_sec - sh_video->pts, audio_delay, 0)){
1757 sh_video->pts = demuxer->video->pts;
1758 //if (vo_vobsub) vobsub_seek(vo_vobsub,sh_video->pts);
1759 resync_video_stream(sh_video);
1760 //if(vo_spudec) spudec_reset(vo_spudec);
1761 if (audio_delay != 0.0) fixdelay(demuxer->video, d_audio, mux_a, frame_data, framecopy);
1762 return 1;
1764 // non-seekable stream.
1765 return 0;
1768 // slow seek, read every frame.
1770 return slowseek(next_edl_record->stop_sec, demuxer->video, d_audio, mux_a, frame_data, framecopy, 1);