synced with r21537
[mplayer/glamo.git] / mencoder.c
blob449111a5aa9503c0486a008e2143580ccb18425e
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"
51 #include "get_path.c"
53 #include "stream/stream.h"
54 #include "libmpdemux/demuxer.h"
55 #include "libmpdemux/stheader.h"
56 #include "libmpdemux/mp3_hdr.h"
57 #include "libmpdemux/muxer.h"
60 #include "libvo/video_out.h"
62 #include "libaf/af_format.h"
64 #include "libmpcodecs/mp_image.h"
65 #include "libmpcodecs/dec_audio.h"
66 #include "libmpcodecs/dec_video.h"
67 #include "libmpcodecs/vf.h"
69 // for MPEGLAYER3WAVEFORMAT:
70 #include "libmpdemux/ms_hdr.h"
72 #include <inttypes.h>
74 #include "libvo/fastmemcpy.h"
76 #include "osdep/timer.h"
78 #ifdef USE_DVDREAD
79 #include "stream/stream_dvd.h"
80 #endif
82 #ifdef USE_LIBAVCODEC
83 #ifdef USE_LIBAVCODEC_SO
84 #include <ffmpeg/avcodec.h>
85 #else
86 #include "avcodec.h"
87 #endif
88 #endif
90 #include "libmpcodecs/ae.h"
91 int vo_doublebuffering=0;
92 int vo_directrendering=0;
93 int vo_config_count=0;
94 int forced_subs_only=0;
96 //--------------------------
98 // cache2:
99 int stream_cache_size=-1;
100 #ifdef USE_STREAM_CACHE
101 extern int cache_fill_status;
103 float stream_cache_min_percent=20.0;
104 float stream_cache_seek_min_percent=50.0;
105 #else
106 #define cache_fill_status 0
107 #endif
109 int audio_id=-1;
110 int video_id=-1;
111 int dvdsub_id=-2;
112 int vobsub_id=-1;
113 char* audio_lang=NULL;
114 char* dvdsub_lang=NULL;
115 static char* spudec_ifo=NULL;
117 static char** audio_codec_list=NULL; // override audio codec
118 static char** video_codec_list=NULL; // override video codec
119 static char** audio_fm_list=NULL; // override audio codec family
120 static char** video_fm_list=NULL; // override video codec family
121 extern char *demuxer_name; // override demuxer
122 extern char *audio_demuxer_name; // override audio demuxer
123 extern char *sub_demuxer_name; // override sub demuxer
125 static int out_audio_codec=-1;
126 static int out_video_codec=-1;
128 int out_file_format=MUXER_TYPE_AVI; // default to AVI
130 // audio stream skip/resync functions requires only for seeking.
131 // (they should be implemented in the audio codec layer)
132 //void skip_audio_frame(sh_audio_t *sh_audio){}
133 //void resync_audio_stream(sh_audio_t *sh_audio){}
135 extern int verbose; // must be global!
136 int quiet=0;
137 double video_time_usage=0;
138 double vout_time_usage=0;
139 double max_video_time_usage=0;
140 double max_vout_time_usage=0;
141 double cur_video_time_usage=0;
142 double cur_vout_time_usage=0;
143 int benchmark=0;
145 extern int mp_msg_levels[MSGT_MAX];
146 extern int mp_msg_level_all;
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 float force_fps=0;
165 static float force_ofps=0; // set to 24 for inverse telecine
166 static int skip_limit=-1;
167 float playback_speed=1.0;
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 #ifdef HAVE_FONTCONFIG
192 extern int font_fontconfig;
193 #endif
194 float font_factor=0.75;
195 char **sub_name=NULL;
196 float sub_delay=0;
197 float sub_fps=0;
198 int sub_auto = 0;
199 int subcc_enabled=0;
200 int suboverlap_enabled = 1;
202 sub_data* subdata=NULL;
203 float sub_last_pts = -303;
205 int auto_expand=1;
206 int encode_duplicates=1;
208 // infos are empty by default
209 char *info_name=NULL;
210 char *info_artist=NULL;
211 char *info_genre=NULL;
212 char *info_subject=NULL;
213 char *info_copyright=NULL;
214 char *info_sourceform=NULL;
215 char *info_comment=NULL;
217 // Needed by libmpdemux.
218 int mp_input_check_interrupt(int time) {
219 usec_sleep(time);
220 return 0;
223 #ifdef USE_ASS
224 #include "libass/ass.h"
225 #include "libass/ass_mp.h"
227 ass_track_t* ass_track = 0; // current track to render
228 #endif
230 //char *out_audio_codec=NULL; // override audio codec
231 //char *out_video_codec=NULL; // override video codec
233 //#include "libmpeg2/mpeg2.h"
234 //#include "libmpeg2/mpeg2_internal.h"
236 //static int vo_w=0, vo_h=0;
238 //-------------------------- config stuff:
240 m_config_t* mconfig;
242 extern int m_config_parse_config_file(m_config_t* config, char *conffile);
244 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
246 static int cfg_include(m_option_t *conf, char *filename){
247 return m_config_parse_config_file(mconfig, filename);
250 static char *seek_to_sec=NULL;
251 static off_t seek_to_byte=0;
253 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
255 static char * frameno_filename=NULL;
257 //static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width, int height);
259 typedef struct {
260 unsigned char* start;
261 int in_size;
262 float frame_time;
263 int already_read;
264 } s_frame_data;
266 /// Returns a_pts
267 static float calc_a_pts(demux_stream_t *d_audio);
268 /** \brief Seeks audio forward to pts by dumping audio packets
269 \return The current audio pts.
271 static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a);
272 /** \brief Seeks slowly by dumping frames.
273 \return 1 for success, 2 for EOF.
275 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);
276 /// Deletes audio or video as told by -delay to sync
277 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);
279 #include "edl.h"
280 static edl_record_ptr edl_records = NULL; ///< EDL entries memory area
281 static edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
282 static short edl_muted; ///< Stores whether EDL is currently in muted mode.
283 static short edl_seeking; ///< When non-zero, stream is seekable.
284 static short edl_seek_type; ///< When non-zero, frames are discarded instead of seeking.
285 /** \brief Seeks for EDL
286 \return 1 for success, 0 for failure, 2 for EOF.
288 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);
290 #include "cfg-mencoder.h"
292 #ifdef USE_DVDREAD
293 #include "spudec.h"
294 #endif
295 #include "vobsub.h"
297 #include "libao2/audio_out.h"
298 /* FIXME */
299 static void mencoder_exit(int level, const char *how)
301 if (how)
302 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ExitingHow, how);
303 else
304 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_Exiting);
306 exit(level);
309 static void parse_cfgfiles( m_config_t* conf )
311 char *conffile;
312 if ((conffile = get_path("mencoder.conf")) == NULL) {
313 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
314 } else {
315 if (m_config_parse_config_file(conf, conffile) < 0)
316 mencoder_exit(1,MSGTR_ConfigFileError);
317 free(conffile);
322 //---------------------------------------------------------------------------
324 static int dec_audio(sh_audio_t *sh_audio,unsigned char* buffer,int total){
325 int size=0;
326 int at_eof=0;
327 while(size<total && !at_eof){
328 int len=total-size;
329 if(len>MAX_OUTBURST) len=MAX_OUTBURST;
330 if(len>sh_audio->a_out_buffer_size) len=sh_audio->a_out_buffer_size;
331 if(len>sh_audio->a_out_buffer_len){
332 int ret=decode_audio(sh_audio,
333 &sh_audio->a_out_buffer[sh_audio->a_out_buffer_len],
334 len-sh_audio->a_out_buffer_len,
335 sh_audio->a_out_buffer_size-sh_audio->a_out_buffer_len);
336 if(ret>0) sh_audio->a_out_buffer_len+=ret; else at_eof=1;
338 if(len>sh_audio->a_out_buffer_len) len=sh_audio->a_out_buffer_len;
339 memcpy(buffer+size,sh_audio->a_out_buffer,len);
340 sh_audio->a_out_buffer_len-=len; size+=len;
341 if(sh_audio->a_out_buffer_len>0)
342 memcpy(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[len],sh_audio->a_out_buffer_len);
344 return size;
347 //---------------------------------------------------------------------------
349 // this function returns the absoloute time for which MEncoder will switch files or move in the file.
350 // so audio can be cut correctly. -1 if there is no limit.
351 static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v);
353 static int at_eof=0;
354 static int interrupted=0;
356 static void exit_sighandler(int x){
357 at_eof=1;
358 interrupted=2; /* 1 means error */
361 static muxer_t* muxer=NULL;
363 extern void print_wave_header(WAVEFORMATEX *h, int verbose_level);
365 int main(int argc,char* argv[]){
367 stream_t* stream=NULL;
368 stream_t* ostream=NULL;
369 demuxer_t* demuxer=NULL;
370 stream_t* stream2=NULL;
371 demuxer_t* demuxer2=NULL;
372 demux_stream_t *d_audio=NULL;
373 demux_stream_t *d_video=NULL;
374 demux_stream_t *d_dvdsub=NULL;
375 sh_audio_t *sh_audio=NULL;
376 sh_video_t *sh_video=NULL;
377 int file_format=DEMUXER_TYPE_UNKNOWN;
378 int i=DEMUXER_TYPE_UNKNOWN;
379 void *vobsub_writer=NULL;
380 s_frame_data frame_data = { .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
382 uint32_t ptimer_start;
383 uint32_t audiorate=0;
384 uint32_t videorate=0;
385 uint32_t audiosamples=1;
386 uint32_t videosamples=1;
387 uint32_t skippedframes=0;
388 uint32_t duplicatedframes=0;
389 uint32_t badframes=0;
391 muxer_stream_t* mux_a=NULL;
392 muxer_stream_t* mux_v=NULL;
393 off_t muxer_f_size=0;
395 double v_pts_corr=0;
396 double v_timer_corr=0;
398 m_entry_t* filelist = NULL;
399 char* filename=NULL;
401 int decoded_frameno=0;
402 int next_frameno=-1;
403 int curfile=0;
404 int new_srate=0;
405 int fformat=DEMUXER_TYPE_UNKNOWN;
407 unsigned int timer_start=0;
408 ao_data_t ao_data = {0,0,0,0,OUTBURST,-1,0};
410 audio_encoding_params_t aparams;
411 audio_encoder_t *aencoder = NULL;
413 mp_msg_init();
414 mp_msg(MSGT_CPLAYER,MSGL_INFO, "MEncoder " VERSION " (C) 2000-2006 MPlayer Team\n");
416 /* Test for cpu capabilities (and corresponding OS support) for optimizing */
417 GetCpuCaps(&gCpuCaps);
418 #ifdef ARCH_X86
419 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: Type: %d MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
420 gCpuCaps.cpuType,gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
421 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
422 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
423 #ifdef RUNTIME_CPUDETECT
424 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
425 #else
426 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
427 #ifdef HAVE_MMX
428 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
429 #endif
430 #ifdef HAVE_MMX2
431 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
432 #endif
433 #ifdef HAVE_3DNOW
434 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
435 #endif
436 #ifdef HAVE_3DNOWEX
437 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
438 #endif
439 #ifdef HAVE_SSE
440 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
441 #endif
442 #ifdef HAVE_SSE2
443 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
444 #endif
445 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n\n");
446 #endif
447 #endif
449 #if defined(WIN32) && defined(USE_WIN32DLL)
450 set_path_env();
451 #endif /*WIN32 && USE_WIN32DLL*/
453 InitTimer();
455 // check codec.conf
456 if(!codecs_file || !parse_codec_cfg(codecs_file)){
457 if(!parse_codec_cfg(get_path("codecs.conf"))){
458 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
459 if(!parse_codec_cfg(NULL)){
460 mp_msg(MSGT_MENCODER,MSGL_HINT,MSGTR_CopyCodecsConf);
461 mencoder_exit(1,NULL);
463 mp_msg(MSGT_MENCODER,MSGL_V,MSGTR_BuiltinCodecsConf);
468 mconfig = m_config_new();
469 m_config_register_options(mconfig,mencoder_opts);
470 parse_cfgfiles(mconfig);
471 filelist = m_config_parse_me_command_line(mconfig, argc, argv);
472 if(!filelist) mencoder_exit(1, MSGTR_ErrorParsingCommandLine);
475 char *extension;
477 if (!out_filename) mencoder_exit(1,MSGTR_MissingOutputFilename);
478 extension=strrchr(out_filename,'.');
479 if (extension != NULL && strlen(extension) > 3 && strlen(extension) < 6)
481 extension++;
483 switch (out_file_format)
485 case MUXER_TYPE_AVI:
486 if (strcasecmp(extension,"avi"))
487 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_MencoderWrongFormatAVI);
488 break;
490 case MUXER_TYPE_MPEG:
491 if (strcasecmp(extension,"mpg") &&
492 strcasecmp(extension,"mpeg") &&
493 strcasecmp(extension,"vob"))
494 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_MencoderWrongFormatMPG);
495 break;
499 /* Display what configure line was used */
500 mp_msg(MSGT_MENCODER, MSGL_V, "Configuration: " CONFIGURATION "\n");
503 if (frameno_filename) {
504 stream2=open_stream(frameno_filename,0,&i);
505 if(stream2){
506 demuxer2=demux_open(stream2,DEMUXER_TYPE_AVI,-1,-1,-2,NULL);
507 if(demuxer2) mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_UsingPass3ControlFile, frameno_filename);
508 else mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized);
512 #ifdef WIN32
513 if(proc_priority){
514 int i;
515 for(i=0; priority_presets_defs[i].name; i++){
516 if(strcasecmp(priority_presets_defs[i].name, proc_priority) == 0)
517 break;
519 mp_msg(MSGT_CPLAYER,MSGL_STATUS,MSGTR_SettingProcessPriority,
520 priority_presets_defs[i].name);
521 SetPriorityClass(GetCurrentProcess(), priority_presets_defs[i].prio);
523 #endif
525 // check font
526 #ifdef HAVE_FREETYPE
527 init_freetype();
528 #endif
529 #ifdef HAVE_FONTCONFIG
530 if(!font_fontconfig)
532 #endif
533 #ifdef HAVE_BITMAP_FONT
534 if(font_name){
535 vo_font=read_font_desc(font_name,font_factor,verbose>1);
536 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,font_name);
537 } else {
538 // try default:
539 vo_font=read_font_desc(get_path("font/font.desc"),font_factor,verbose>1);
540 if(!vo_font)
541 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
543 #endif
544 #ifdef HAVE_FONTCONFIG
546 #endif
548 vo_init_osd();
550 /* HACK, for some weird reason, push() has to be called twice,
551 otherwise options are not saved correctly */
552 m_config_push(mconfig);
553 play_next_file:
554 m_config_push(mconfig);
555 m_entry_set_options(mconfig,&filelist[curfile]);
556 filename = filelist[curfile].name;
558 if(!filename){
559 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_MissingFilename);
560 mencoder_exit(1,NULL);
562 stream=open_stream(filename,0,&file_format);
564 if(!stream){
565 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_CannotOpenFile_Device);
566 mencoder_exit(1,NULL);
569 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_OpenedStream, file_format, (int)(stream->start_pos), (int)(stream->end_pos));
571 #ifdef USE_DVDREAD
572 if(stream->type==STREAMTYPE_DVD){
573 if(audio_lang && audio_id==-1) audio_id=dvd_aid_from_lang(stream,audio_lang);
574 if(dvdsub_lang && dvdsub_id==-2) dvdsub_id=dvd_sid_from_lang(stream,dvdsub_lang);
576 #endif
578 stream->start_pos+=seek_to_byte;
580 if(stream_cache_size>0) stream_enable_cache(stream,stream_cache_size*1024,0,0);
582 if(demuxer2) audio_id=-2; /* do NOT read audio packets... */
584 //demuxer=demux_open(stream,file_format,video_id,audio_id,dvdsub_id);
585 demuxer=demux_open(stream,file_format,audio_id,video_id,dvdsub_id,filename);
586 if(!demuxer){
587 mp_msg(MSGT_DEMUXER, MSGL_FATAL, MSGTR_FormatNotRecognized);
588 mp_msg(MSGT_DEMUXER, MSGL_FATAL, MSGTR_CannotOpenDemuxer);
589 mencoder_exit(1,NULL);
592 d_audio=demuxer2 ? demuxer2->audio : demuxer->audio;
593 d_video=demuxer->video;
594 d_dvdsub=demuxer->sub;
595 sh_audio=d_audio->sh;
596 sh_video=d_video->sh;
598 if(!sh_video)
600 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_VideoStreamRequired);
601 mencoder_exit(1,NULL);
604 if(!video_read_properties(sh_video)){
605 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_CannotReadVideoProperties);
606 mencoder_exit(1,NULL);
609 mp_msg(MSGT_MENCODER,MSGL_INFO, MSGTR_FilefmtFourccSizeFpsFtime,
610 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
611 sh_video->fps,sh_video->frametime
614 if(force_fps){
615 sh_video->fps=force_fps;
616 sh_video->frametime=1.0f/sh_video->fps;
617 mp_msg(MSGT_MENCODER,MSGL_INFO,MSGTR_ForcingInputFPS, sh_video->fps);
620 if(sh_audio && out_audio_codec<0){
621 if(audio_id==-2)
622 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_DemuxerDoesntSupportNosound);
623 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoAudioEncoderSelected);
624 mencoder_exit(1,NULL);
626 if(sh_video && out_video_codec<0){
627 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoVideoEncoderSelected);
628 mencoder_exit(1,NULL);
631 if(sh_audio && (out_audio_codec || seek_to_sec || !sh_audio->wf || playback_speed != 1.0)){
632 // Go through the codec.conf and find the best codec...
633 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
634 if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
635 sh_audio=d_audio->sh=NULL; // failed to init :(
637 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
640 if (sh_audio) {
641 new_srate = sh_audio->samplerate;
642 if (playback_speed != 1.0) {
643 new_srate *= playback_speed;
644 // limits are taken from libaf/af_resample.c
645 if (new_srate < 8000) new_srate = 8000;
646 if (new_srate > 192000) new_srate = 192000;
647 playback_speed = (float)new_srate / (float)sh_audio->samplerate;
651 // after reading video params we should load subtitles because
652 // we know fps so now we can adjust subtitles time to ~6 seconds AST
653 // check .sub
654 // current_module="read_subtitles_file";
655 if(sub_name && sub_name[0]){
656 subdata=sub_read_file(sub_name[0], sh_video->fps);
657 if(!subdata) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,sub_name[0]);
658 } else
659 if(sub_auto && filename) { // auto load sub file ...
660 char **tmp = NULL;
661 int i = 0;
662 char *psub = get_path( "sub/" );
663 tmp = sub_filenames((psub ? psub : ""), filename);
664 free(psub);
665 subdata=sub_read_file(tmp[0], sh_video->fps);
666 while (tmp[i])
667 free(tmp[i++]);
668 free(tmp);
671 // set up video encoder:
673 if (!curfile) { // curfile is non zero when a second file is opened
674 if (vobsub_out) {
675 unsigned int palette[16], width, height;
676 unsigned char tmp[3] = { 0, 0, 0 };
677 if (spudec_ifo && vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, dvdsub_id, tmp) >= 0)
678 vobsub_writer = vobsub_out_open(vobsub_out, palette, sh_video->disp_w, sh_video->disp_h,
679 vobsub_out_id?vobsub_out_id:(char *)tmp, vobsub_out_index);
680 #ifdef USE_DVDREAD
681 if (vobsub_writer == NULL) {
682 char tmp[3];
683 if (vobsub_out_id == NULL && stream->type == STREAMTYPE_DVD) {
684 int i;
685 dvd_priv_t *dvd = (dvd_priv_t*)stream->priv;
686 for (i = 0; i < dvd->nr_of_subtitles; ++i)
687 if (dvd->subtitles[i].id == dvdsub_id) {
688 tmp[0] = (dvd->subtitles[i].language >> 8) & 0xff;
689 tmp[1] = dvd->subtitles[i].language & 0xff;
690 tmp[2] = 0;
691 vobsub_out_id = tmp;
692 break;
695 vobsub_writer=vobsub_out_open(vobsub_out, stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
696 sh_video->disp_w, sh_video->disp_h, vobsub_out_id, vobsub_out_index);
698 #endif
700 else {
701 if (spudec_ifo) {
702 unsigned int palette[16], width, height;
703 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
704 vo_spudec=spudec_new_scaled(palette, sh_video->disp_w, sh_video->disp_h);
706 #ifdef USE_DVDREAD
707 if (vo_spudec==NULL) {
708 vo_spudec=spudec_new_scaled(stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
709 sh_video->disp_w, sh_video->disp_h);
711 #endif
714 // Apply current settings for forced subs
715 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
717 ostream = open_output_stream(out_filename, 0);
718 if(!ostream) {
719 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CannotOpenOutputFile, out_filename);
720 mencoder_exit(1,NULL);
723 muxer=muxer_new_muxer(out_file_format,ostream);
724 if(!muxer) {
725 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CannotInitializeMuxer);
726 mencoder_exit(1,NULL);
728 #if 0
729 //disabled: it horrybly distorts filtered sound
730 if(out_file_format == MUXER_TYPE_MPEG) audio_preload = 0;
731 #endif
733 muxer->audio_delay_fix = audio_delay_fix;
735 // ============= VIDEO ===============
737 mux_v=muxer_new_stream(muxer,MUXER_TYPE_VIDEO);
739 mux_v->buffer_size=0x200000; // 2MB
740 mux_v->buffer=malloc(mux_v->buffer_size);
742 mux_v->source=sh_video;
744 mux_v->h.dwSampleSize=0; // VBR
745 #ifdef USE_LIBAVCODEC
747 AVRational q= av_d2q(force_ofps?force_ofps:sh_video->fps*playback_speed, 30000);
748 mux_v->h.dwScale= q.den;
749 mux_v->h.dwRate = q.num;
751 #else
752 mux_v->h.dwScale=10000;
753 mux_v->h.dwRate=mux_v->h.dwScale*(force_ofps?force_ofps:sh_video->fps*playback_speed);
754 #endif
756 mux_v->codec=out_video_codec;
758 mux_v->bih=NULL;
760 sh_video->codec=NULL;
761 sh_video->video_out=NULL;
762 sh_video->vfilter=NULL; // fixme!
764 switch(mux_v->codec){
765 case VCODEC_COPY:
766 if (!curfile) {
767 if (sh_video->bih) {
768 mux_v->bih=malloc(sh_video->bih->biSize);
769 memcpy(mux_v->bih, sh_video->bih, sh_video->bih->biSize);
771 else
773 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
774 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
775 mux_v->bih->biWidth=sh_video->disp_w;
776 mux_v->bih->biHeight=sh_video->disp_h;
777 mux_v->bih->biCompression=sh_video->format;
778 mux_v->bih->biPlanes=1;
779 mux_v->bih->biBitCount=24; // FIXME!!!
780 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
783 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
784 mux_v->bih->biWidth, mux_v->bih->biHeight,
785 mux_v->bih->biBitCount, mux_v->bih->biCompression);
787 if (curfile) {
788 if (sh_video->bih) {
789 if ((mux_v->bih->biSize != sh_video->bih->biSize) ||
790 memcmp(mux_v->bih, sh_video->bih, sh_video->bih->biSize))
792 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
793 sh_video->bih->biWidth, sh_video->bih->biHeight,
794 sh_video->bih->biBitCount, sh_video->bih->biCompression);
795 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_FrameCopyFileMismatch);
796 mencoder_exit(1,NULL);
799 else {
800 if ((mux_v->bih->biWidth != sh_video->disp_w) ||
801 (mux_v->bih->biHeight != sh_video->disp_h) ||
802 (mux_v->bih->biCompression != sh_video->format)) {
803 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
804 sh_video->disp_w, sh_video->disp_w, 24, sh_video->format);
805 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_FrameCopyFileMismatch);
806 mencoder_exit(1,NULL);
810 break;
811 case VCODEC_FRAMENO:
812 if (!curfile) {
813 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
814 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
815 mux_v->bih->biWidth=sh_video->disp_w;
816 mux_v->bih->biHeight=sh_video->disp_h;
817 mux_v->bih->biPlanes=1;
818 mux_v->bih->biBitCount=24;
819 mux_v->bih->biCompression=mmioFOURCC('F','r','N','o');
820 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
822 break;
823 default: {
824 static vf_instance_t * ve = NULL;
825 if (!ve) {
826 switch(mux_v->codec){
827 case VCODEC_LIBAVCODEC:
828 sh_video->vfilter=vf_open_encoder(NULL,"lavc",(char *)mux_v); break;
829 case VCODEC_RAW:
830 sh_video->vfilter=vf_open_encoder(NULL,"raw",(char *)mux_v); break;
831 case VCODEC_VFW:
832 sh_video->vfilter=vf_open_encoder(NULL,"vfw",(char *)mux_v); break;
833 case VCODEC_LIBDV:
834 sh_video->vfilter=vf_open_encoder(NULL,"libdv",(char *)mux_v); break;
835 case VCODEC_XVID:
836 sh_video->vfilter=vf_open_encoder(NULL,"xvid",(char *)mux_v); break;
837 case VCODEC_QTVIDEO:
838 sh_video->vfilter=vf_open_encoder(NULL,"qtvideo",(char *)mux_v); break;
839 case VCODEC_NUV:
840 sh_video->vfilter=vf_open_encoder(NULL,"nuv",(char *)mux_v); break;
841 case VCODEC_X264:
842 sh_video->vfilter=vf_open_encoder(NULL,"x264",(char *)mux_v); break;
844 if(!mux_v->bih || !sh_video->vfilter){
845 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_EncoderOpenFailed);
846 mencoder_exit(1,NULL);
848 ve = sh_video->vfilter;
849 } else sh_video->vfilter = ve;
850 // append 'expand' filter, it fixes stride problems and renders osd:
851 if (auto_expand) {
852 char* vf_args[] = { "osd", "1", NULL };
853 sh_video->vfilter=vf_open_filter(sh_video->vfilter,"expand",vf_args);
855 sh_video->vfilter=append_filters(sh_video->vfilter);
857 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
858 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
859 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
860 if(!sh_video->inited) mencoder_exit(1,NULL);
864 if (!curfile) {
865 /* force output fourcc to .. */
866 if ((force_fourcc != NULL) && (strlen(force_fourcc) >= 4))
868 mux_v->bih->biCompression = mmioFOURCC(force_fourcc[0], force_fourcc[1],
869 force_fourcc[2], force_fourcc[3]);
870 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ForcingOutputFourcc,
871 mux_v->bih->biCompression, (char *)&mux_v->bih->biCompression);
874 if (! ignore_start)
875 muxer->audio_delay_fix -= sh_video->stream_delay;
877 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
879 // ============= AUDIO ===============
880 if(sh_audio){
882 if (force_audiofmttag != -1) {
883 sh_audio->format = force_audiofmttag;
884 if (sh_audio->wf) {
885 sh_audio->wf->wFormatTag = sh_audio->format;
887 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ForcingOutputAudiofmtTag,
888 force_audiofmttag);
891 mux_a=muxer_new_stream(muxer,MUXER_TYPE_AUDIO);
893 mux_a->buffer_size=0x100000; //16384;
894 mux_a->buffer=malloc(mux_a->buffer_size);
895 if (!mux_a->buffer)
896 mencoder_exit(1,MSGTR_MemAllocFailed);
898 mux_a->source=sh_audio;
900 mux_a->codec=out_audio_codec;
902 ao_data.samplerate = force_srate;
903 ao_data.channels = 0;
904 ao_data.format = audio_output_format;
905 if(!preinit_audio_filters(sh_audio,
906 // input:
907 new_srate,
908 sh_audio->channels, sh_audio->sample_format,
909 // output:
910 &ao_data.samplerate, &ao_data.channels, &ao_data.format)) {
911 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
912 mencoder_exit(1, NULL);
915 aparams.channels = ao_data.channels;
916 aparams.sample_rate = ao_data.samplerate;
917 aparams.audio_preload = 1000 * audio_preload;
918 if(mux_a->codec != ACODEC_COPY) {
919 aencoder = new_audio_encoder(mux_a, &aparams);
920 if(!aencoder)
921 mencoder_exit(1, NULL);
922 if(!init_audio_filters(sh_audio,
923 new_srate, sh_audio->channels, sh_audio->sample_format,
924 &aparams.sample_rate, &aparams.channels, &aencoder->input_format,
925 aencoder->min_buffer_size, aencoder->max_buffer_size)) {
926 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_NoMatchingFilter);
927 mencoder_exit(1,NULL);
930 switch(mux_a->codec){
931 case ACODEC_COPY:
932 if (playback_speed != 1.0) mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_NoSpeedWithFrameCopy);
933 if (sh_audio->format >= 0x10000) {
934 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantCopyAudioFormat, sh_audio->format);
935 mencoder_exit(1,NULL);
937 if (sh_audio->wf){
938 mux_a->wf=malloc(sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
939 memcpy(mux_a->wf, sh_audio->wf, sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
940 if(!sh_audio->i_bps) sh_audio->i_bps=mux_a->wf->nAvgBytesPerSec;
941 } else {
942 mux_a->wf = malloc(sizeof(WAVEFORMATEX));
943 mux_a->wf->nBlockAlign = 1; //mux_a->h.dwSampleSize;
944 mux_a->wf->wFormatTag = sh_audio->format;
945 mux_a->wf->nChannels = sh_audio->channels;
946 mux_a->wf->nSamplesPerSec = sh_audio->samplerate;
947 mux_a->wf->nAvgBytesPerSec=sh_audio->i_bps; //mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
948 mux_a->wf->wBitsPerSample = 16; // FIXME
949 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
951 if(sh_audio->audio.dwScale){
952 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize;
953 mux_a->h.dwScale=sh_audio->audio.dwScale;
954 mux_a->h.dwRate=sh_audio->audio.dwRate;
955 } else {
956 mux_a->h.dwSampleSize=mux_a->wf->nBlockAlign;
957 mux_a->h.dwScale=mux_a->h.dwSampleSize;
958 mux_a->h.dwRate=mux_a->wf->nAvgBytesPerSec;
960 mux_a->h.dwRate *= playback_speed;
961 mux_a->wf->nSamplesPerSec *= playback_speed;
962 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
963 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
964 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
965 break;
968 if ( mp_msg_test(MSGT_MENCODER,MSGL_DBG2) ) print_wave_header(mux_a->wf, MSGL_DBG2);
970 if (! ignore_start)
971 muxer->audio_delay_fix += sh_audio->stream_delay;
973 } // if(sh_audio)
975 decoded_frameno=0;
977 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
978 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
979 signal(SIGTERM,exit_sighandler); // kill
980 signal(SIGHUP,exit_sighandler); // broken terminal line
981 signal(SIGPIPE,exit_sighandler); // broken pipe
983 timer_start=GetTimerMS();
984 } // if (!curfile) // if this was the first file.
985 else {
986 if (!mux_a != !sh_audio) {
987 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoAudioFileMismatch);
988 mencoder_exit(1,NULL);
990 if (sh_audio && mux_a->codec == ACODEC_COPY) {
991 if (playback_speed != 1.0) mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_NoSpeedWithFrameCopy);
992 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
993 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
994 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
995 if (sh_audio->wf) {
996 if ((mux_a->wf->wFormatTag != sh_audio->wf->wFormatTag) ||
997 (mux_a->wf->nChannels != sh_audio->wf->nChannels) ||
998 (mux_a->wf->nSamplesPerSec != sh_audio->wf->nSamplesPerSec * playback_speed))
1000 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
1001 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * playback_speed),
1002 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1003 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_AudioCopyFileMismatch);
1004 mencoder_exit(1,NULL);
1006 } else {
1007 if ((mux_a->wf->wFormatTag != sh_audio->format) ||
1008 (mux_a->wf->nChannels != sh_audio->channels) ||
1009 (mux_a->wf->nSamplesPerSec != sh_audio->samplerate * playback_speed))
1011 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
1012 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * playback_speed),
1013 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1014 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_AudioCopyFileMismatch);
1015 mencoder_exit(1,NULL);
1018 } else if (sh_audio) {
1019 int out_srate = mux_a->wf->nSamplesPerSec;
1020 int out_channels = mux_a->wf->nChannels;
1021 int out_format = aencoder->input_format;
1022 int out_minsize = aencoder->min_buffer_size;
1023 int out_maxsize = aencoder->max_buffer_size;
1024 if (!init_audio_filters(sh_audio, new_srate, sh_audio->channels,
1025 sh_audio->sample_format, &out_srate, &out_channels,
1026 &out_format, out_minsize, out_maxsize)) {
1027 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoMatchingFilter);
1028 mencoder_exit(1, NULL);
1030 mux_a->wf->nSamplesPerSec = out_srate;
1031 mux_a->wf->nChannels = out_channels;
1035 if (seek_to_sec) {
1036 int a,b; float d;
1038 if (sscanf(seek_to_sec, "%d:%d:%f", &a,&b,&d)==3)
1039 d += 3600*a + 60*b;
1040 else if (sscanf(seek_to_sec, "%d:%f", &a, &d)==2)
1041 d += 60*a;
1042 else
1043 sscanf(seek_to_sec, "%f", &d);
1045 demux_seek(demuxer, d, audio_delay, 1);
1046 // there is 2 way to handle the -ss option in 3-pass mode:
1047 // > 1. do the first pass for the whole file, and use -ss for 2nd/3rd pases only
1048 // > 2. do all the 3 passes with the same -ss value
1049 // this line enables behaviour 1. (and kills 2. at the same time):
1050 // if(demuxer2) demux_seek(demuxer2, d, audio_delay, 1);
1053 if (out_file_format == MUXER_TYPE_MPEG)
1055 if (audio_preload > 0.4) {
1056 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_LimitingAudioPreload);
1057 audio_preload = 0.4;
1059 if (audio_density < 4) {
1060 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_IncreasingAudioDensity);
1061 audio_density = 4;
1065 if(file_format == DEMUXER_TYPE_TV)
1067 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_ZeroingAudioPreloadAndMaxPtsCorrection);
1068 audio_preload = 0.0;
1069 default_max_pts_correction = 0;
1072 play_n_frames=play_n_frames_mf;
1073 if (curfile && end_at.type == END_AT_TIME) end_at.pos += mux_v->timer;
1075 if (edl_records) free_edl(edl_records);
1076 next_edl_record = edl_records = NULL;
1077 edl_muted = 0;
1078 edl_seeking = 1;
1079 if (edl_filename) {
1080 next_edl_record = edl_records = edl_parse_file();
1083 if (sh_audio && audio_delay != 0.) fixdelay(d_video, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1085 while(!at_eof){
1087 int blit_frame=0;
1088 float a_pts=0;
1089 float v_pts=0;
1090 int skip_flag=0; // 1=skip -1=duplicate
1092 if((end_at.type == END_AT_SIZE && end_at.pos <= stream_tell(muxer->stream)) ||
1093 (end_at.type == END_AT_TIME && end_at.pos < mux_v->timer))
1094 break;
1096 if(play_n_frames>=0){
1097 --play_n_frames;
1098 if(play_n_frames<0) break;
1101 goto_redo_edl:
1102 if (next_edl_record && sh_video && sh_video->pts >= next_edl_record->start_sec) {
1103 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1104 float last_pos = d_video->pts;
1105 int result;
1106 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop [%f], length [%f]\n",
1107 next_edl_record->start_sec, next_edl_record->stop_sec, next_edl_record->length_sec);
1109 result = edl_seek(next_edl_record, demuxer, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1111 if (result == 2) { at_eof=1; break; } // EOF
1112 else if (result == 0) edl_seeking = 0; // no seeking
1113 else { // sucess
1114 edl_muted = 0;
1115 if (last_pos >= sh_video->pts) {
1116 // backwards seek detected!! Forget about this EDL skip altogether.
1117 next_edl_record = next_edl_record->next;
1119 else for (next_edl_record = edl_records; next_edl_record; next_edl_record = next_edl_record->next) {
1120 /* note the use of stop_sec,
1121 meaning if by some magical way we landed in the MIDDLE of a censored area,
1122 in the next loop it will jump out of it.
1124 if (next_edl_record->stop_sec > sh_video->pts) break; // we got to the right place.
1125 if (next_edl_record->action == EDL_MUTE) edl_muted = !edl_muted; // toggle mute each time.
1128 /* for a pedantic EDL, that doesn't show even a single
1129 frame from the "censored" area, uncomment next line. */
1130 goto goto_redo_edl;
1132 } else if (next_edl_record->action == EDL_MUTE) {
1133 edl_muted = !edl_muted; // This variable does nothing for now.
1134 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n", next_edl_record->start_sec );
1135 next_edl_record=next_edl_record->next;
1140 if(sh_audio){
1141 // get audio:
1142 while(mux_a->timer-audio_preload<mux_v->timer){
1143 float tottime;
1144 int len=0;
1146 ptimer_start = GetTimerMS();
1147 // CBR - copy 0.5 sec of audio
1148 // or until the end of video:
1149 tottime = stop_time(demuxer, mux_v);
1150 if (tottime != -1) {
1151 tottime -= mux_a->timer;
1152 if (tottime > 1./audio_density) tottime = 1./audio_density;
1154 else tottime = 1./audio_density;
1156 // let's not output more audio than necessary
1157 if (tottime <= 0) break;
1159 if(aencoder)
1161 if(mux_a->h.dwSampleSize) /* CBR */
1163 if(aencoder->set_decoded_len)
1165 len = mux_a->h.dwSampleSize*(int)(mux_a->h.dwRate*tottime);
1166 aencoder->set_decoded_len(aencoder, len);
1168 else
1169 len = aencoder->decode_buffer_size;
1171 len = dec_audio(sh_audio, aencoder->decode_buffer, len);
1172 mux_a->buffer_len += aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len,
1173 aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1174 if(mux_a->buffer_len < mux_a->wf->nBlockAlign)
1175 len = 0;
1176 else
1177 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1179 else /* VBR */
1181 int sz = 0;
1182 while(1)
1184 len = 0;
1185 if(! sz)
1186 sz = aencoder->get_frame_size(aencoder);
1187 if(sz > 0 && mux_a->buffer_len >= sz)
1189 len = sz;
1190 break;
1192 len = dec_audio(sh_audio,aencoder->decode_buffer, aencoder->decode_buffer_size);
1193 if(len <= 0)
1195 len = 0;
1196 break;
1198 len = aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len, aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1199 mux_a->buffer_len += len;
1202 if (mux_v->timer == 0) mux_a->h.dwInitialFrames++;
1204 else {
1205 if(mux_a->h.dwSampleSize){
1206 switch(mux_a->codec){
1207 case ACODEC_COPY: // copy
1208 len=mux_a->wf->nAvgBytesPerSec*tottime;
1209 len/=mux_a->h.dwSampleSize;if(len<1) len=1;
1210 len*=mux_a->h.dwSampleSize;
1211 len=demux_read_data(sh_audio->ds,mux_a->buffer,len);
1212 break;
1214 } else {
1215 // VBR - encode/copy an audio frame
1216 switch(mux_a->codec){
1217 case ACODEC_COPY: // copy
1218 len=ds_get_packet(sh_audio->ds,(unsigned char**) &mux_a->buffer);
1219 break;
1223 if(len<=0) break; // EOF?
1224 muxer_write_chunk(mux_a,len,0x10, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1225 if(!mux_a->h.dwSampleSize && mux_a->timer>0)
1226 mux_a->wf->nAvgBytesPerSec=0.5f+(double)mux_a->size/mux_a->timer; // avg bps (VBR)
1227 if(mux_a->buffer_len>=len){
1228 mux_a->buffer_len-=len;
1229 memcpy(mux_a->buffer,mux_a->buffer+len,mux_a->buffer_len);
1233 audiosamples++;
1234 audiorate+= (GetTimerMS() - ptimer_start);
1239 // get video frame!
1241 if (!frame_data.already_read) {
1242 frame_data.in_size=video_read_frame(sh_video,&frame_data.frame_time,&frame_data.start,force_fps);
1243 sh_video->timer+=frame_data.frame_time;
1245 frame_data.frame_time /= playback_speed;
1246 if(frame_data.in_size<0){ at_eof=1; break; }
1247 ++decoded_frameno;
1249 v_timer_corr-=frame_data.frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate;
1251 if(demuxer2){ // 3-pass encoding, read control file (frameno.avi)
1252 // find our frame:
1253 while(next_frameno<decoded_frameno){
1254 int* start;
1255 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1256 if(len<0){ at_eof=1;break;}
1257 if(len==0) --skip_flag; else // duplicate
1258 if(len==4) next_frameno=start[0];
1260 if(at_eof) break;
1261 // if(skip_flag) printf("!!!!!!!!!!!!\n");
1262 skip_flag=next_frameno-decoded_frameno;
1263 // find next frame:
1264 while(next_frameno<=decoded_frameno){
1265 int* start;
1266 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1267 if(len<0){ at_eof=1;break;}
1268 if(len==0) --skip_flag; else // duplicate
1269 if(len==4) next_frameno=start[0];
1271 // if(at_eof) break;
1272 // printf("Current fno=%d requested=%d skip=%d \n",decoded_frameno,fno,skip_flag);
1273 } else {
1275 // check frame duplicate/drop:
1277 //printf("\r### %5.3f ###\n",v_timer_corr);
1278 float mux_frametime = (float)mux_v->h.dwScale/mux_v->h.dwRate;
1280 if (v_timer_corr >= mux_frametime && (skip_limit<0 || skip_flag < skip_limit)) {
1281 v_timer_corr-=mux_frametime;
1282 ++skip_flag; // skip
1284 while (v_timer_corr <= -mux_frametime && (skip_limit<0 || -skip_flag < skip_limit)) {
1285 v_timer_corr+=mux_frametime;
1286 --skip_flag; // dup
1289 // either v_pts_corr is big, more than 2 times framerate, then we follow its advice,
1290 // or, it cancels out v_timer_corr, in which case be happy and do nothing.
1292 while ((v_pts_corr <= -mux_frametime && skip_flag > 0) || (v_pts_corr <= -2*mux_frametime)) {
1293 v_pts_corr+=mux_frametime;
1294 --skip_flag; // dup
1296 if ((v_pts_corr >= mux_frametime && skip_flag < 0) || (v_pts_corr >= 2*mux_frametime)) {
1297 if (skip_flag<=0) { // we can't skip more than 1 frame now
1298 v_pts_corr-=mux_frametime;
1299 ++skip_flag; // skip
1303 } // demuxer2
1305 ptimer_start = GetTimerMS();
1307 switch(mux_v->codec){
1308 case VCODEC_COPY:
1309 mux_v->buffer=frame_data.start;
1310 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);
1311 break;
1312 case VCODEC_FRAMENO:
1313 mux_v->buffer=(unsigned char *)&decoded_frameno; // tricky
1314 if(skip_flag<=0) muxer_write_chunk(mux_v,sizeof(int),0x10, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1315 break;
1316 default:
1317 // decode_video will callback down to ve_*.c encoders, through the video filters
1318 {void *decoded_frame = decode_video(sh_video,frame_data.start,frame_data.in_size,
1319 skip_flag>0 && (!sh_video->vfilter || ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) != CONTROL_TRUE), MP_NOPTS_VALUE);
1320 blit_frame = decoded_frame && filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE);}
1322 if (sh_video->vf_inited < 0) mencoder_exit(1, NULL);
1324 if(!blit_frame){
1325 if (play_n_frames >= 0)
1326 play_n_frames++;
1327 badframes++;
1328 if(skip_flag<=0){
1329 // unwanted skipping of a frame, what to do?
1330 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1331 #if 0
1332 // Old code apparently made under the assumption that !blit_frame means
1333 // decoding failed due to corruption or something.. but duplication and
1334 // skipping of frames should be entirely disabled when skip_limit==0,
1335 // and must be in order for many filters to work with -noskip.
1336 // Eventually this entire block should probably be removed.
1337 if(skip_limit==0){
1338 // skipping not allowed -> write empty frame:
1339 if (!encode_duplicates || !sh_video->vfilter || ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1340 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1341 } else {
1342 // skipping allowed -> skip it and distriubute timer error:
1343 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1345 #endif
1350 videosamples++;
1351 videorate+=(GetTimerMS() - ptimer_start);
1353 if(skip_flag<0){
1354 // duplicate frame
1355 if(!quiet) mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_DuplicateFrames,-skip_flag);
1356 while(skip_flag<0){
1357 duplicatedframes++;
1358 if (!encode_duplicates || !sh_video->vfilter || ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1359 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1360 ++skip_flag;
1362 } else
1363 if(skip_flag>0){
1364 // skip frame
1365 if(!quiet) mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_SkipFrame);
1366 skippedframes++;
1367 --skip_flag;
1370 if(sh_audio && !demuxer2){
1371 float AV_delay,x;
1372 // A-V sync!
1373 #if 0
1374 if(pts_from_bps){
1375 unsigned int samples=(sh_audio->audio.dwSampleSize)?
1376 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
1377 (d_audio->block_no); // <- used for VBR audio
1378 // printf("samples=%d \n",samples);
1379 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
1380 delay_corrected=1;
1381 } else
1382 #endif
1384 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
1385 a_pts=d_audio->pts;
1386 if(!delay_corrected) if(a_pts) delay_corrected=1;
1387 //printf("*** %5.3f ***\n",a_pts);
1388 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1390 v_pts=sh_video ? sh_video->pts : d_video->pts;
1391 // av = compensated (with out buffering delay) A-V diff
1392 AV_delay=(a_pts-v_pts);
1393 AV_delay-=audio_delay;
1394 AV_delay /= playback_speed;
1395 AV_delay-=mux_a->timer-(mux_v->timer-(v_timer_corr+v_pts_corr));
1396 // adjust for encoder delays
1397 AV_delay -= (float) mux_a->encoder_delay * mux_a->h.dwScale/mux_a->h.dwRate;
1398 AV_delay += (float) mux_v->encoder_delay * mux_v->h.dwScale/mux_v->h.dwRate;
1399 // compensate input video timer by av:
1400 x=AV_delay*0.1f;
1401 if(x<-max_pts_correction) x=-max_pts_correction; else
1402 if(x> max_pts_correction) x= max_pts_correction;
1403 if(default_max_pts_correction>=0)
1404 max_pts_correction=default_max_pts_correction*playback_speed;
1405 else
1406 max_pts_correction=sh_video->frametime*0.10 *playback_speed; // +-10% of time
1407 // sh_video->timer-=x;
1408 c_total+=x;
1409 v_pts_corr+=x;
1412 // printf("A:%6.1f V:%6.1f A-V:%7.3f oAV:%7.3f diff:%7.3f ct:%7.3f vpc:%7.3f \r",
1413 // a_pts,v_pts,a_pts-v_pts,
1414 // (float)(mux_a->timer-mux_v->timer),
1415 // AV_delay, c_total, v_pts_corr );
1416 // printf("V:%6.1f \r", d_video->pts );
1418 #if 0
1419 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",
1420 a_pts,v_pts,a_pts-v_pts,c_total,
1421 (int)sh_video->num_frames,(int)sh_video->num_frames_decoded,
1422 (sh_video->timer>0.5)?(int)(100.0*video_time_usage/(double)sh_video->timer):0,
1423 (sh_video->timer>0.5)?(int)(100.0*vout_time_usage/(double)sh_video->timer):0,
1424 (sh_video->timer>0.5)?(100.0*audio_time_usage/(double)sh_video->timer):0
1425 ,cache_fill_status
1427 #endif
1429 { float t=(GetTimerMS()-timer_start)*0.001f;
1430 float len=(demuxer->movi_end-demuxer->movi_start);
1431 float p=len>1000 ? (float)(demuxer->filepos-demuxer->movi_start) / len :
1432 (demuxer_get_percent_pos(demuxer) / 100.0);
1433 #if 0
1434 if(!len && sh_audio && sh_audio->audio.dwLength>100){
1435 p=(sh_audio->audio.dwSampleSize? ds_tell(sh_audio->ds)/sh_audio->audio.dwSampleSize : sh_audio->ds->block_no)
1436 / (float)(sh_audio->audio.dwLength);
1438 #endif
1439 #if 0
1440 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"%d < %d < %d \r",
1441 (int)demuxer->movi_start,
1442 (int)demuxer->filepos,
1443 (int)demuxer->movi_end);
1444 #else
1445 if(!quiet) {
1446 if( mp_msg_test(MSGT_AVSYNC,MSGL_V) ) {
1447 mp_msg(MSGT_AVSYNC,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",
1448 mux_v->timer, decoded_frameno, (int)(p*100),
1449 (t>1) ? (int)(decoded_frameno/t+0.5) : 0,
1450 (p>0.001) ? (int)((t/p-t)/60) : 0,
1451 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1452 v_pts_corr,
1453 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1454 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0,
1455 audiorate/audiosamples, videorate/videosamples,
1456 duplicatedframes, badframes, skippedframes
1458 } else
1459 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"Pos:%6.1fs %6df (%2d%%) %5.2ffps Trem:%4dmin %3dmb A-V:%5.3f [%d:%d]\r",
1460 mux_v->timer, decoded_frameno, (int)(p*100),
1461 (t>1) ? (float)(decoded_frameno/t) : 0,
1462 (p>0.001) ? (int)((t/p-t)/60) : 0,
1463 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1464 v_pts_corr,
1465 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1466 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0
1469 #endif
1471 fflush(stdout);
1473 // find sub
1474 if(subdata && sh_video->pts>0){
1475 float pts=sh_video->pts;
1476 if(sub_fps==0) sub_fps=sh_video->fps;
1477 if (pts > sub_last_pts || pts < sub_last_pts-1.0 ) {
1478 find_sub(subdata, (pts+sub_delay) *
1479 (subdata->sub_uses_time? 100. : sub_fps));
1480 // FIXME! frame counter...
1481 sub_last_pts = pts;
1485 #ifdef USE_DVDREAD
1486 // DVD sub:
1487 if(vo_spudec||vobsub_writer){
1488 unsigned char* packet=NULL;
1489 int len;
1490 while((len=ds_get_packet_sub(d_dvdsub,&packet))>0){
1491 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);
1492 if (vo_spudec)
1493 spudec_assemble(vo_spudec,packet,len,90000*d_dvdsub->pts);
1494 if (vobsub_writer)
1495 vobsub_out_output(vobsub_writer,packet,len,mux_v->timer + d_dvdsub->pts - sh_video->pts);
1497 if (vo_spudec) {
1498 spudec_heartbeat(vo_spudec,90000*sh_video->pts);
1499 vo_osd_changed(OSDTYPE_SPU);
1502 #endif
1504 frame_data = (s_frame_data){ .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
1506 #if 0
1507 if(ferror(muxer_f)) {
1508 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile, out_filename);
1509 mencoder_exit(1, NULL);
1511 #endif
1513 } // while(!at_eof)
1515 if (!interrupted && filelist[++curfile].name != 0) {
1516 if (sh_video && sh_video->vfilter) { // Before uniniting sh_video and the filter chain, break apart the VE.
1517 vf_instance_t * ve; // this will be the filter right before the ve.
1518 for (ve = sh_video->vfilter; ve->next && ve->next->next; ve = ve->next);
1520 if (ve->next) ve->next = NULL; // I'm telling the last filter, before the VE, there is nothing after it
1521 else sh_video->vfilter = NULL; // There is no chain except the VE.
1524 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1525 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1526 if(demuxer) free_demuxer(demuxer);
1527 if(stream) free_stream(stream); // kill cache thread
1529 at_eof = 0;
1531 m_config_pop(mconfig);
1532 goto play_next_file;
1535 /* Emit the remaining frames in the video system */
1536 /*TODO emit frmaes delayed by decoder lag*/
1537 if(sh_video && sh_video->vfilter){
1538 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_FlushingVideoFrames);
1539 if (!((vf_instance_t *)sh_video->vfilter)->fmt.have_configured)
1540 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_FiltersHaveNotBeenConfiguredEmptyFile);
1541 else
1542 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter,
1543 VFCTRL_FLUSH_FRAMES, 0);
1546 if(aencoder)
1547 if(aencoder->fixup)
1548 aencoder->fixup(aencoder);
1550 if (muxer->cont_write_index) muxer_write_index(muxer);
1551 muxer_f_size=stream_tell(muxer->stream);
1552 stream_seek(muxer->stream,0);
1553 if (muxer->cont_write_header) muxer_write_header(muxer); // update header
1554 #if 0
1555 if(ferror(muxer_f) || fclose(muxer_f) != 0) {
1556 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile, out_filename);
1557 mencoder_exit(1, NULL);
1559 #endif
1560 if(vobsub_writer)
1561 vobsub_out_close(vobsub_writer);
1563 if(out_video_codec==VCODEC_FRAMENO && mux_v->timer>100){
1564 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"650MB",(int)((650*1024*1024-muxer_f_size)/mux_v->timer/125));
1565 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"700MB",(int)((700*1024*1024-muxer_f_size)/mux_v->timer/125));
1566 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"800MB",(int)((800*1024*1024-muxer_f_size)/mux_v->timer/125));
1567 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"2 x 650MB",(int)((2*650*1024*1024-muxer_f_size)/mux_v->timer/125));
1568 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"2 x 700MB",(int)((2*700*1024*1024-muxer_f_size)/mux_v->timer/125));
1569 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"2 x 800MB",(int)((2*800*1024*1024-muxer_f_size)/mux_v->timer/125));
1572 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VideoStreamResult,
1573 (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);
1574 if(sh_audio)
1575 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_AudioStreamResult,
1576 (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);
1578 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1579 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1580 if(demuxer) free_demuxer(demuxer);
1581 if(stream) free_stream(stream); // kill cache thread
1583 return interrupted;
1587 #if 0
1588 /* Flip the image in src and store the result in dst. src and dst may overlap.
1589 width is the size of each line in bytes. */
1590 static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width,
1591 int height)
1593 uint8_t* tmp = malloc(width);
1594 int i;
1596 for(i = 0; i < height/2; i++) {
1597 memcpy(tmp, &src[i*width], width);
1598 memcpy(&dst[i * width], &src[(height - i) * width], width);
1599 memcpy(&dst[(height - i) * width], tmp, width);
1602 free(tmp);
1603 return dst;
1605 #endif
1608 static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v) {
1609 float timeleft = -1;
1610 if (play_n_frames >= 0) timeleft = mux_v->timer + play_n_frames * (double)(mux_v->h.dwScale) / mux_v->h.dwRate;
1611 if (end_at.type == END_AT_TIME && (timeleft > end_at.pos || timeleft == -1)) timeleft = end_at.pos;
1612 if (next_edl_record && demuxer && demuxer->video) { // everything is OK to be checked
1613 float tmp = mux_v->timer + next_edl_record->start_sec - demuxer->video->pts;
1614 if (timeleft == -1 || timeleft > tmp) {
1615 // There's less time in EDL than what we already know
1616 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1617 timeleft = tmp;
1618 } else if (next_edl_record->action == EDL_MUTE) {
1619 //timeleft = next_edl_record->start_sec - demuxer->video->pts;
1620 // For the moment (and probably forever) EDL mute doesn't work in MEncoder
1624 return timeleft;
1627 static float calc_a_pts(demux_stream_t *d_audio) {
1628 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
1629 float a_pts = 0.;
1630 if (sh_audio)
1631 a_pts = d_audio->pts + (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1632 return a_pts;
1635 static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a) {
1636 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
1637 int samplesize, avg;
1638 float a_pts = calc_a_pts(d_audio);
1640 if (!sh_audio) return a_pts;
1642 if (sh_audio->audio.dwScale) samplesize = sh_audio->audio.dwSampleSize;
1643 else samplesize = (sh_audio->wf ? sh_audio->wf->nBlockAlign : 1);
1644 avg = (sh_audio->wf ? sh_audio->wf->nAvgBytesPerSec : sh_audio->i_bps);
1646 // after a demux_seek, a_pts will be zero until you read some audio.
1647 // carefully checking if a_pts is truely correct by reading tiniest amount of data possible.
1648 if (pts > a_pts && a_pts == 0.0 && samplesize) {
1649 if (demux_read_data(sh_audio->ds,mux_a->buffer,samplesize) <= 0) return a_pts; // EOF
1650 a_pts = calc_a_pts(d_audio);
1653 while (pts > a_pts) {
1654 int len;
1655 if (samplesize) {
1656 len = avg * (pts - a_pts > 0.5 ? 0.5 : pts - a_pts);
1657 len/= samplesize; if(len<1) len=1;
1658 len*= samplesize;
1659 len = demux_read_data(sh_audio->ds,mux_a->buffer,len);
1660 } else {
1661 unsigned char * crap;
1662 len = ds_get_packet(sh_audio->ds, &crap);
1664 if (len <= 0) break; // EOF of audio.
1665 a_pts = calc_a_pts(d_audio);
1667 return a_pts;
1670 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) {
1671 sh_video_t * sh_video = d_video->sh;
1672 vf_instance_t * vfilter = sh_video ? sh_video->vfilter : NULL;
1673 int done = 0;
1675 while (!interrupted) {
1676 float a_pts = 0.;
1678 if (!frame_data->already_read) { // when called after fixdelay, a frame is already read
1679 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
1680 if(frame_data->in_size<0) return 2;
1681 sh_video->timer += frame_data->frame_time;
1683 frame_data->already_read = 0;
1685 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
1687 if (done) {
1688 frame_data->already_read = 1;
1689 if (!framecopy || (sh_video->ds->flags & 1)) return 1;
1691 if (sh_video->pts >= end_pts) done = 1;
1693 if (vfilter) {
1694 int softskip = (vfilter->control(vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) == CONTROL_TRUE);
1695 void *decoded_frame = decode_video(sh_video, frame_data->start, frame_data->in_size, !softskip, MP_NOPTS_VALUE);
1696 if (decoded_frame)
1697 filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE);
1700 if (print_info) mp_msg(MSGT_MENCODER, MSGL_STATUS,
1701 MSGTR_EdlSkipStartEndCurrent,
1702 next_edl_record->start_sec, next_edl_record->stop_sec,
1703 sh_video->pts, a_pts);
1705 if (interrupted) return 2;
1706 return 1;
1709 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) {
1710 // TODO: Find a way to encode silence instead of deleting video
1711 sh_video_t * sh_video = d_video->sh;
1712 float a_pts;
1714 // demux_seek has a weirdness that sh_video->pts is meaningless,
1715 // until a single frame is read... Same for audio actually too.
1716 // Reading one frame, and keeping it.
1717 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
1718 if(frame_data->in_size<0) return;
1719 sh_video->timer += frame_data->frame_time;
1720 frame_data->already_read = 1;
1722 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
1724 if (audio_delay > 0) return;
1725 else if (sh_video->pts - frame_data->frame_time + audio_delay >= a_pts) return;
1727 slowseek(a_pts - audio_delay, d_video, d_audio, mux_a, frame_data, framecopy, 0);
1730 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) {
1731 sh_video_t * sh_video = demuxer->video ? demuxer->video->sh : NULL;
1733 if (!sh_video) return 0;
1734 if (sh_video->pts >= next_edl_record->stop_sec) return 1; // nothing to do...
1736 if (!edl_seek_type) {
1737 if(demux_seek(demuxer, next_edl_record->stop_sec - sh_video->pts, audio_delay, 0)){
1738 sh_video->pts = demuxer->video->pts;
1739 //if (vo_vobsub) vobsub_seek(vo_vobsub,sh_video->pts);
1740 resync_video_stream(sh_video);
1741 //if(vo_spudec) spudec_reset(vo_spudec);
1742 if (audio_delay != 0.0) fixdelay(demuxer->video, d_audio, mux_a, frame_data, framecopy);
1743 return 1;
1745 // non-seekable stream.
1746 return 0;
1749 // slow seek, read every frame.
1751 return slowseek(next_edl_record->stop_sec, demuxer->video, d_audio, mux_a, frame_data, framecopy, 1);