Remove useless malloc casts.
[mplayer.git] / mencoder.c
blob1265c0b227ab814858d61fd43d70182c99a49f9d
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #define VCODEC_COPY 0
20 #define VCODEC_FRAMENO 1
21 // real codecs:
22 #define VCODEC_LIBAVCODEC 4
23 #define VCODEC_VFW 7
24 #define VCODEC_LIBDV 8
25 #define VCODEC_XVID 9
26 #define VCODEC_QTVIDEO 10
27 #define VCODEC_NUV 11
28 #define VCODEC_RAW 12
29 #define VCODEC_X264 13
31 #define ACODEC_COPY 0
32 #define ACODEC_PCM 1
33 #define ACODEC_VBRMP3 2
34 #define ACODEC_NULL 3
35 #define ACODEC_LAVC 4
36 #define ACODEC_TOOLAME 5
37 #define ACODEC_FAAC 6
38 #define ACODEC_TWOLAME 7
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <signal.h>
44 #include "config.h"
46 #ifdef __MINGW32__
47 #define SIGHUP 1
48 #define SIGQUIT 3
49 #define SIGPIPE 13
50 #endif
51 #if defined(__MINGW32__) || defined(__CYGWIN__)
52 #include <windows.h>
53 #endif
55 #include <sys/time.h>
57 #include "mp_msg.h"
58 #include "help_mp.h"
60 #include "codec-cfg.h"
61 #include "m_option.h"
62 #include "m_config.h"
63 #include "parser-mecmd.h"
64 #include "parser-cfg.h"
66 #include "get_path.h"
68 #include "stream/stream.h"
69 #include "libmpdemux/demuxer.h"
70 #include "libmpdemux/stheader.h"
71 #include "libmpdemux/mp3_hdr.h"
72 #include "libmpdemux/muxer.h"
75 #include "libvo/video_out.h"
77 #include "libaf/af_format.h"
79 #include "libmpcodecs/mp_image.h"
80 #include "libmpcodecs/dec_audio.h"
81 #include "libmpcodecs/dec_video.h"
82 #include "libmpcodecs/vf.h"
83 #include "libmpcodecs/vd.h"
85 // for MPEGLAYER3WAVEFORMAT:
86 #include "libmpdemux/ms_hdr.h"
88 #include <inttypes.h>
90 #include "libvo/fastmemcpy.h"
92 #include "osdep/timer.h"
94 #ifdef CONFIG_DVDREAD
95 #include "stream/stream_dvd.h"
96 #endif
98 #include "stream/stream_dvdnav.h"
99 #include "libavcodec/avcodec.h"
101 #include "libmpcodecs/ae.h"
102 int vo_doublebuffering=0;
103 int vo_directrendering=0;
104 int vo_config_count=1;
105 int forced_subs_only=0;
107 //--------------------------
109 // cache2:
110 int stream_cache_size=-1;
111 #ifdef CONFIG_STREAM_CACHE
112 extern int cache_fill_status;
114 float stream_cache_min_percent=20.0;
115 float stream_cache_seek_min_percent=50.0;
116 #else
117 #define cache_fill_status 0
118 #endif
120 int audio_id=-1;
121 int video_id=-1;
122 int dvdsub_id=-1;
123 int vobsub_id=-1;
124 char* audio_lang=NULL;
125 char* dvdsub_lang=NULL;
126 static char* spudec_ifo=NULL;
128 static char** audio_codec_list=NULL; // override audio codec
129 static char** video_codec_list=NULL; // override video codec
130 static char** audio_fm_list=NULL; // override audio codec family
131 static char** video_fm_list=NULL; // override video codec family
132 extern char *demuxer_name; // override demuxer
133 extern char *audio_demuxer_name; // override audio demuxer
134 extern char *sub_demuxer_name; // override sub demuxer
136 static int out_audio_codec=-1;
137 static int out_video_codec=-1;
139 int out_file_format=MUXER_TYPE_AVI; // default to AVI
141 // audio stream skip/resync functions requires only for seeking.
142 // (they should be implemented in the audio codec layer)
143 //void skip_audio_frame(sh_audio_t *sh_audio){}
144 //void resync_audio_stream(sh_audio_t *sh_audio){}
146 int quiet=0;
147 double video_time_usage=0;
148 double vout_time_usage=0;
149 double max_video_time_usage=0;
150 double max_vout_time_usage=0;
151 double cur_video_time_usage=0;
152 double cur_vout_time_usage=0;
153 int benchmark=0;
155 #include "osdep/priority.h"
157 // A-V sync:
158 int delay_corrected=1;
159 static float default_max_pts_correction=-1;//0.01f;
160 static float max_pts_correction=0;//default_max_pts_correction;
161 static float c_total=0;
163 static float audio_preload=0.5;
164 static float audio_delay_fix=0.0;
165 static float audio_delay=0.0;
166 static int ignore_start=0;
167 static int audio_density=2;
169 double force_fps=0;
170 static double force_ofps=0; // set to 24 for inverse telecine
171 static int skip_limit=-1;
172 float playback_speed=1.0;
174 static int force_srate=0;
175 static int audio_output_format=0;
177 char *vobsub_out=NULL;
178 unsigned int vobsub_out_index=0;
179 char *vobsub_out_id=NULL;
181 char* out_filename=NULL;
183 char *force_fourcc=NULL;
184 int force_audiofmttag=-1;
186 char* passtmpfile="divx2pass.log";
188 static int play_n_frames=-1;
189 static int play_n_frames_mf=-1;
191 #include "libvo/font_load.h"
192 #include "libvo/sub.h"
194 // sub:
195 char *font_name=NULL;
196 char *sub_font_name=NULL;
197 extern int font_fontconfig;
198 float font_factor=0.75;
199 char **sub_name=NULL;
200 float sub_delay=0;
201 float sub_fps=0;
202 int sub_auto = 0;
203 int subcc_enabled=0;
204 int suboverlap_enabled = 1;
206 int auto_expand=1;
207 int encode_duplicates=1;
209 // infos are empty by default
210 char *info_name=NULL;
211 char *info_artist=NULL;
212 char *info_genre=NULL;
213 char *info_subject=NULL;
214 char *info_copyright=NULL;
215 char *info_sourceform=NULL;
216 char *info_comment=NULL;
218 // Needed by libmpcodecs vf_vo.c
219 int config_video_out(const vo_functions_t *vo, uint32_t width, uint32_t height,
220 uint32_t d_width, uint32_t d_height, uint32_t flags,
221 char *title, uint32_t format) {
222 return 1;
224 // Needed by libmpdemux.
225 int mp_input_check_interrupt(int time) {
226 usec_sleep(time);
227 return 0;
229 // Needed by getch2
230 void mplayer_put_key(int code)
234 #include "libass/ass_mp.h"
235 char *current_module;
236 #include "mpcommon.h"
238 // Needed by mpcommon.c
239 void set_osd_subtitle(subtitle *subs) {
240 vo_sub = subs;
241 vo_osd_changed(OSDTYPE_SUBTITLE);
244 //char *out_audio_codec=NULL; // override audio codec
245 //char *out_video_codec=NULL; // override video codec
247 //#include "libmpeg2/mpeg2.h"
248 //#include "libmpeg2/mpeg2_internal.h"
250 //static int vo_w=0, vo_h=0;
252 //-------------------------- config stuff:
254 m_config_t* mconfig;
256 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
258 static int cfg_include(m_option_t *conf, char *filename){
259 return m_config_parse_config_file(mconfig, filename);
262 static double seek_to_sec;
263 static off_t seek_to_byte=0;
265 static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
267 static char * frameno_filename=NULL;
269 //static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width, int height);
271 typedef struct {
272 unsigned char* start;
273 int in_size;
274 float frame_time;
275 int already_read;
276 } s_frame_data;
278 /// Returns a_pts
279 static float calc_a_pts(demux_stream_t *d_audio);
280 /** \brief Seeks audio forward to pts by dumping audio packets
281 \return The current audio pts.
283 static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a);
284 /** \brief Seeks slowly by dumping frames.
285 \return 1 for success, 2 for EOF.
287 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);
288 /// Deletes audio or video as told by -delay to sync
289 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);
291 #include "edl.h"
292 static edl_record_ptr edl_records = NULL; ///< EDL entries memory area
293 static edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
294 static short edl_muted; ///< Stores whether EDL is currently in muted mode.
295 static short edl_seeking; ///< When non-zero, stream is seekable.
296 static short edl_seek_type; ///< When non-zero, frames are discarded instead of seeking.
297 /** \brief Seeks for EDL
298 \return 1 for success, 0 for failure, 2 for EOF.
300 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);
302 #include "cfg-mencoder.h"
304 #include "spudec.h"
305 #include "vobsub.h"
307 #include "libao2/audio_out.h"
308 /* FIXME */
309 static void mencoder_exit(int level, const char *how)
311 if (how)
312 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ExitingHow, how);
313 else
314 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_Exiting);
316 exit(level);
319 static void parse_cfgfiles( m_config_t* conf )
321 char *conffile;
322 if (!disable_system_conf &&
323 m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mencoder.conf") < 0)
324 mencoder_exit(1,MSGTR_ConfigFileError);
326 if (!disable_user_conf) {
327 if ((conffile = get_path("mencoder.conf")) == NULL) {
328 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
329 } else {
330 if (m_config_parse_config_file(conf, conffile) < 0)
331 mencoder_exit(1,MSGTR_ConfigFileError);
332 free(conffile);
338 //---------------------------------------------------------------------------
340 static int dec_audio(sh_audio_t *sh_audio,unsigned char* buffer,int total){
341 int size=0;
342 int at_eof=0;
343 while(size<total && !at_eof){
344 int len=total-size;
345 if(len>MAX_OUTBURST) len=MAX_OUTBURST;
346 if (decode_audio(sh_audio, len) < 0) at_eof=1;
347 if(len>sh_audio->a_out_buffer_len) len=sh_audio->a_out_buffer_len;
348 fast_memcpy(buffer+size,sh_audio->a_out_buffer,len);
349 sh_audio->a_out_buffer_len-=len; size+=len;
350 if(sh_audio->a_out_buffer_len>0)
351 fast_memcpy(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[len],sh_audio->a_out_buffer_len);
353 return size;
356 //---------------------------------------------------------------------------
358 // this function returns the absoloute time for which MEncoder will switch files or move in the file.
359 // so audio can be cut correctly. -1 if there is no limit.
360 static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v);
362 static volatile int at_eof=0;
363 static volatile int interrupted=0;
365 static void exit_sighandler(int x){
366 at_eof=1;
367 interrupted=2; /* 1 means error */
370 static muxer_t* muxer=NULL;
372 void print_wave_header(WAVEFORMATEX *h, int verbose_level);
374 int main(int argc,char* argv[]){
376 stream_t* stream=NULL;
377 stream_t* ostream=NULL;
378 demuxer_t* demuxer=NULL;
379 stream_t* stream2=NULL;
380 demuxer_t* demuxer2=NULL;
381 demux_stream_t *d_audio=NULL;
382 demux_stream_t *d_video=NULL;
383 demux_stream_t *d_dvdsub=NULL;
384 sh_audio_t *sh_audio=NULL;
385 sh_video_t *sh_video=NULL;
386 int file_format=DEMUXER_TYPE_UNKNOWN;
387 int i=DEMUXER_TYPE_UNKNOWN;
388 void *vobsub_writer=NULL;
389 s_frame_data frame_data = { .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
391 uint32_t ptimer_start;
392 uint32_t audiorate=0;
393 uint32_t videorate=0;
394 uint32_t audiosamples=1;
395 uint32_t videosamples=1;
396 uint32_t skippedframes=0;
397 uint32_t duplicatedframes=0;
398 uint32_t badframes=0;
400 muxer_stream_t* mux_a=NULL;
401 muxer_stream_t* mux_v=NULL;
402 off_t muxer_f_size=0;
404 double v_pts_corr=0;
405 double v_timer_corr=0;
407 m_entry_t* filelist = NULL;
408 char* filename=NULL;
410 int decoded_frameno=0;
411 int next_frameno=-1;
412 int curfile=0;
413 int new_srate=0;
415 unsigned int timer_start=0;
416 ao_data_t ao_data = {0,0,0,0,OUTBURST,-1,0};
418 audio_encoding_params_t aparams;
419 audio_encoder_t *aencoder = NULL;
421 user_correct_pts = 0;
423 mp_msg_init();
425 // Create the config context and register the options
426 mconfig = m_config_new();
427 m_config_register_options(mconfig,mencoder_opts);
429 // Preparse the command line
430 m_config_preparse_command_line(mconfig,argc,argv);
432 print_version("MEncoder");
434 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && defined(CONFIG_WIN32DLL)
435 set_path_env();
436 #endif
438 InitTimer();
440 // check codec.conf
441 if(!codecs_file || !parse_codec_cfg(codecs_file)){
442 if(!parse_codec_cfg(get_path("codecs.conf"))){
443 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
444 if(!parse_codec_cfg(NULL)){
445 mencoder_exit(1,NULL);
447 mp_msg(MSGT_MENCODER,MSGL_V,MSGTR_BuiltinCodecsConf);
452 parse_cfgfiles(mconfig);
453 filelist = m_config_parse_me_command_line(mconfig, argc, argv);
454 if(!filelist) mencoder_exit(1, MSGTR_ErrorParsingCommandLine);
457 char *extension;
459 if (!out_filename) mencoder_exit(1,MSGTR_MissingOutputFilename);
460 extension=strrchr(out_filename,'.');
461 if (extension != NULL && strlen(extension) > 3 && strlen(extension) < 6)
463 extension++;
465 switch (out_file_format)
467 case MUXER_TYPE_AVI:
468 if (strcasecmp(extension,"avi"))
469 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_MencoderWrongFormatAVI);
470 break;
472 case MUXER_TYPE_MPEG:
473 if (strcasecmp(extension,"mpg") &&
474 strcasecmp(extension,"mpeg") &&
475 strcasecmp(extension,"vob"))
476 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_MencoderWrongFormatMPG);
477 break;
481 /* Display what configure line was used */
482 mp_msg(MSGT_MENCODER, MSGL_V, "Configuration: " CONFIGURATION "\n");
485 if (frameno_filename) {
486 stream2=open_stream(frameno_filename,0,&i);
487 if(stream2){
488 demuxer2=demux_open(stream2,DEMUXER_TYPE_AVI,-1,-1,-2,NULL);
489 if(demuxer2) mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_UsingPass3ControlFile, frameno_filename);
490 else mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized);
494 #ifdef CONFIG_PRIORITY
495 set_priority();
496 #endif
498 // check font
499 #ifdef CONFIG_FREETYPE
500 init_freetype();
501 #endif
502 #ifdef CONFIG_FONTCONFIG
503 if(font_fontconfig <= 0)
505 #endif
506 #ifdef CONFIG_BITMAP_FONT
507 if(font_name){
508 vo_font=read_font_desc(font_name,font_factor,verbose>1);
509 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,font_name);
510 } else {
511 // try default:
512 vo_font=read_font_desc(get_path("font/font.desc"),font_factor,verbose>1);
513 if(!vo_font)
514 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
516 #endif
517 #ifdef CONFIG_FONTCONFIG
519 #endif
521 vo_init_osd();
523 /* HACK, for some weird reason, push() has to be called twice,
524 otherwise options are not saved correctly */
525 m_config_push(mconfig);
526 play_next_file:
527 m_config_push(mconfig);
528 m_entry_set_options(mconfig,&filelist[curfile]);
529 filename = filelist[curfile].name;
531 if(!filename){
532 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_MissingFilename);
533 mencoder_exit(1,NULL);
535 stream=open_stream(filename,0,&file_format);
537 if(!stream){
538 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_CannotOpenFile_Device);
539 mencoder_exit(1,NULL);
542 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_OpenedStream, file_format, (int)(stream->start_pos), (int)(stream->end_pos));
544 #ifdef CONFIG_DVDREAD
545 if(stream->type==STREAMTYPE_DVD){
546 if(audio_lang && audio_id==-1) audio_id=dvd_aid_from_lang(stream,audio_lang);
547 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(stream,dvdsub_lang);
549 #endif
551 #ifdef CONFIG_DVDNAV
552 if(stream->type==STREAMTYPE_DVDNAV){
553 if(audio_lang && audio_id==-1) audio_id=mp_dvdnav_aid_from_lang(stream,audio_lang);
554 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=mp_dvdnav_sid_from_lang(stream,dvdsub_lang);
556 #endif
558 stream->start_pos+=seek_to_byte;
560 if(stream_cache_size>0) stream_enable_cache(stream,stream_cache_size*1024,0,0);
562 if(demuxer2) audio_id=-2; /* do NOT read audio packets... */
564 //demuxer=demux_open(stream,file_format,video_id,audio_id,dvdsub_id);
565 demuxer=demux_open(stream,file_format,audio_id,video_id,dvdsub_id,filename);
566 if(!demuxer){
567 mp_msg(MSGT_DEMUXER, MSGL_FATAL, MSGTR_FormatNotRecognized);
568 mp_msg(MSGT_DEMUXER, MSGL_FATAL, MSGTR_CannotOpenDemuxer);
569 mencoder_exit(1,NULL);
572 if (ts_prog) {
573 demux_program_t prog = { .progid = ts_prog };
574 if (demux_control(demuxer, DEMUXER_CTRL_IDENTIFY_PROGRAM, &prog) != DEMUXER_CTRL_NOTIMPL) {
575 audio_id = prog.aid; // switching is handled by select_audio below
576 video_id = prog.vid;
577 demuxer_switch_video(demuxer, video_id);
580 select_audio(demuxer, audio_id, audio_lang);
582 if (dvdsub_id == -1 && dvdsub_lang)
583 dvdsub_id = demuxer_sub_track_by_lang(demuxer, dvdsub_lang);
585 if (dvdsub_id == -1)
586 dvdsub_id = demuxer_default_sub_track(demuxer);
588 for (i = 0; i < MAX_S_STREAMS; i++) {
589 sh_sub_t *sh = demuxer->s_streams[i];
590 if (sh && sh->sid == dvdsub_id) {
591 demuxer->sub->id = i;
592 demuxer->sub->sh = sh;
593 break;
597 if(dvd_chapter>1) {
598 float pts;
599 if (demuxer_seek_chapter(demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
600 seek_to_sec = pts;
603 d_audio=demuxer2 ? demuxer2->audio : demuxer->audio;
604 d_video=demuxer->video;
605 d_dvdsub=demuxer->sub;
606 sh_audio=d_audio->sh;
607 sh_video=d_video->sh;
609 if(!sh_video)
611 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_VideoStreamRequired);
612 mencoder_exit(1,NULL);
615 if(!video_read_properties(sh_video)){
616 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_CannotReadVideoProperties);
617 mencoder_exit(1,NULL);
620 mp_msg(MSGT_MENCODER,MSGL_INFO, MSGTR_FilefmtFourccSizeFpsFtime,
621 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
622 sh_video->fps,sh_video->frametime
625 if(force_fps){
626 sh_video->fps=force_fps;
627 sh_video->frametime=1.0f/sh_video->fps;
628 mp_msg(MSGT_MENCODER,MSGL_INFO,MSGTR_ForcingInputFPS, sh_video->fps);
631 if(sh_audio && out_audio_codec<0){
632 if(audio_id==-2)
633 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_DemuxerDoesntSupportNosound);
634 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoAudioEncoderSelected);
635 mencoder_exit(1,NULL);
637 if(sh_video && out_video_codec<0){
638 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoVideoEncoderSelected);
639 mencoder_exit(1,NULL);
642 if(sh_audio && (out_audio_codec || seek_to_sec || !sh_audio->wf || playback_speed != 1.0)){
643 // Go through the codec.conf and find the best codec...
644 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
645 if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
646 sh_audio=d_audio->sh=NULL; // failed to init :(
648 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
651 if (sh_audio) {
652 new_srate = sh_audio->samplerate;
653 if (playback_speed != 1.0) {
654 new_srate *= playback_speed;
655 // limits are taken from libaf/af_resample.c
656 if (new_srate < 8000) new_srate = 8000;
657 if (new_srate > 192000) new_srate = 192000;
658 playback_speed = (float)new_srate / (float)sh_audio->samplerate;
662 // after reading video params we should load subtitles because
663 // we know fps so now we can adjust subtitles time to ~6 seconds AST
664 // check .sub
665 // current_module="read_subtitles_file";
666 if(sub_name && sub_name[0]){
667 subdata=sub_read_file(sub_name[0], sh_video->fps);
668 if(!subdata) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,sub_name[0]);
669 } else
670 if(sub_auto && filename) { // auto load sub file ...
671 char **tmp = NULL;
672 int i = 0;
673 char *psub = get_path( "sub/" );
674 tmp = sub_filenames((psub ? psub : ""), filename);
675 free(psub);
676 subdata=sub_read_file(tmp[0], sh_video->fps);
677 while (tmp[i])
678 free(tmp[i++]);
679 free(tmp);
682 // set up video encoder:
684 if (!curfile) { // curfile is non zero when a second file is opened
685 if (vobsub_out) {
686 unsigned int palette[16], width, height;
687 unsigned char tmp[3] = { 0, 0, 0 };
688 if (spudec_ifo && vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, dvdsub_id, tmp) >= 0)
689 vobsub_writer = vobsub_out_open(vobsub_out, palette, sh_video->disp_w, sh_video->disp_h,
690 vobsub_out_id?vobsub_out_id:(char *)tmp, vobsub_out_index);
691 #ifdef CONFIG_DVDREAD
692 if (vobsub_writer == NULL) {
693 char tmp[3];
694 if (vobsub_out_id == NULL && stream->type == STREAMTYPE_DVD) {
695 int i;
696 dvd_priv_t *dvd = (dvd_priv_t*)stream->priv;
697 for (i = 0; i < dvd->nr_of_subtitles; ++i)
698 if (dvd->subtitles[i].id == dvdsub_id) {
699 tmp[0] = (dvd->subtitles[i].language >> 8) & 0xff;
700 tmp[1] = dvd->subtitles[i].language & 0xff;
701 tmp[2] = 0;
702 vobsub_out_id = tmp;
703 break;
706 vobsub_writer=vobsub_out_open(vobsub_out, stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
707 sh_video->disp_w, sh_video->disp_h, vobsub_out_id, vobsub_out_index);
709 #endif
711 else {
712 if (spudec_ifo) {
713 unsigned int palette[16], width, height;
714 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
715 vo_spudec=spudec_new_scaled(palette, sh_video->disp_w, sh_video->disp_h, NULL, 0);
717 #ifdef CONFIG_DVDREAD
718 if (vo_spudec==NULL) {
719 vo_spudec=spudec_new_scaled(stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
720 sh_video->disp_w, sh_video->disp_h, NULL, 0);
722 #endif
723 if (vo_spudec)
724 spudec_set_forced_subs_only(vo_spudec, forced_subs_only);
727 ostream = open_output_stream(out_filename, 0);
728 if(!ostream) {
729 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CannotOpenOutputFile, out_filename);
730 mencoder_exit(1,NULL);
733 muxer=muxer_new_muxer(out_file_format,ostream);
734 if(!muxer) {
735 mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CannotInitializeMuxer);
736 mencoder_exit(1,NULL);
738 #if 0
739 //disabled: it horrybly distorts filtered sound
740 if(out_file_format == MUXER_TYPE_MPEG) audio_preload = 0;
741 #endif
743 muxer->audio_delay_fix = audio_delay_fix;
745 // ============= VIDEO ===============
747 mux_v=muxer_new_stream(muxer,MUXER_TYPE_VIDEO);
749 mux_v->buffer_size=0x200000; // 2MB
750 mux_v->buffer=malloc(mux_v->buffer_size);
752 mux_v->source=sh_video;
754 mux_v->h.dwSampleSize=0; // VBR
755 #ifdef CONFIG_LIBAVCODEC
757 double fps = force_ofps?force_ofps:sh_video->fps*playback_speed;
758 AVRational q= av_d2q(fps, fps*1001+2);
759 mux_v->h.dwScale= q.den;
760 mux_v->h.dwRate = q.num;
762 #else
763 mux_v->h.dwScale=10000;
764 mux_v->h.dwRate=mux_v->h.dwScale*(force_ofps?force_ofps:sh_video->fps*playback_speed);
765 #endif
767 mux_v->codec=out_video_codec;
769 mux_v->bih=NULL;
771 sh_video->codec=NULL;
772 sh_video->vfilter=NULL; // fixme!
774 switch(mux_v->codec){
775 case VCODEC_COPY:
776 if (!curfile) {
777 if (sh_video->bih) {
778 mux_v->bih=malloc(sh_video->bih->biSize);
779 memcpy(mux_v->bih, sh_video->bih, sh_video->bih->biSize);
781 else
783 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
784 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
785 mux_v->bih->biWidth=sh_video->disp_w;
786 mux_v->bih->biHeight=sh_video->disp_h;
787 mux_v->bih->biCompression=sh_video->format;
788 mux_v->bih->biPlanes=1;
789 mux_v->bih->biBitCount=24; // FIXME!!!
790 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
793 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
794 mux_v->bih->biWidth, mux_v->bih->biHeight,
795 mux_v->bih->biBitCount, mux_v->bih->biCompression);
797 if (curfile) {
798 if (sh_video->bih) {
799 if ((mux_v->bih->biSize != sh_video->bih->biSize) ||
800 memcmp(mux_v->bih, sh_video->bih, sh_video->bih->biSize))
802 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
803 sh_video->bih->biWidth, sh_video->bih->biHeight,
804 sh_video->bih->biBitCount, sh_video->bih->biCompression);
805 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_FrameCopyFileMismatch);
806 mencoder_exit(1,NULL);
809 else {
810 if ((mux_v->bih->biWidth != sh_video->disp_w) ||
811 (mux_v->bih->biHeight != sh_video->disp_h) ||
812 (mux_v->bih->biCompression != sh_video->format)) {
813 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
814 sh_video->disp_w, sh_video->disp_w, 24, sh_video->format);
815 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_FrameCopyFileMismatch);
816 mencoder_exit(1,NULL);
820 break;
821 case VCODEC_FRAMENO:
822 if (!curfile) {
823 mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
824 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
825 mux_v->bih->biWidth=sh_video->disp_w;
826 mux_v->bih->biHeight=sh_video->disp_h;
827 mux_v->bih->biPlanes=1;
828 mux_v->bih->biBitCount=24;
829 mux_v->bih->biCompression=mmioFOURCC('F','r','N','o');
830 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
832 break;
833 default: {
834 static vf_instance_t * ve = NULL;
835 if (!ve) {
836 switch(mux_v->codec){
837 case VCODEC_LIBAVCODEC:
838 sh_video->vfilter=vf_open_encoder(NULL,"lavc",(char *)mux_v); break;
839 case VCODEC_RAW:
840 sh_video->vfilter=vf_open_encoder(NULL,"raw",(char *)mux_v); break;
841 case VCODEC_VFW:
842 sh_video->vfilter=vf_open_encoder(NULL,"vfw",(char *)mux_v); break;
843 case VCODEC_LIBDV:
844 sh_video->vfilter=vf_open_encoder(NULL,"libdv",(char *)mux_v); break;
845 case VCODEC_XVID:
846 sh_video->vfilter=vf_open_encoder(NULL,"xvid",(char *)mux_v); break;
847 case VCODEC_QTVIDEO:
848 sh_video->vfilter=vf_open_encoder(NULL,"qtvideo",(char *)mux_v); break;
849 case VCODEC_NUV:
850 sh_video->vfilter=vf_open_encoder(NULL,"nuv",(char *)mux_v); break;
851 case VCODEC_X264:
852 sh_video->vfilter=vf_open_encoder(NULL,"x264",(char *)mux_v); break;
854 if(!mux_v->bih || !sh_video->vfilter){
855 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_EncoderOpenFailed);
856 mencoder_exit(1,NULL);
858 ve = sh_video->vfilter;
859 } else sh_video->vfilter = ve;
860 // append 'expand' filter, it fixes stride problems and renders osd:
861 if (auto_expand) {
862 char* vf_args[] = { "osd", "1", NULL };
863 sh_video->vfilter=vf_open_filter(sh_video->vfilter,"expand",vf_args);
865 sh_video->vfilter=append_filters(sh_video->vfilter);
867 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
868 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
869 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
870 if(!sh_video->initialized) mencoder_exit(1,NULL);
874 if (!curfile) {
875 /* force output fourcc to .. */
876 if ((force_fourcc != NULL) && (strlen(force_fourcc) >= 4))
878 mux_v->bih->biCompression = mmioFOURCC(force_fourcc[0], force_fourcc[1],
879 force_fourcc[2], force_fourcc[3]);
880 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ForcingOutputFourcc,
881 mux_v->bih->biCompression, (char *)&mux_v->bih->biCompression);
884 if (! ignore_start)
885 muxer->audio_delay_fix -= sh_video->stream_delay;
887 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
889 // ============= AUDIO ===============
890 if(sh_audio){
892 if (force_audiofmttag != -1) {
893 sh_audio->format = force_audiofmttag;
894 if (sh_audio->wf) {
895 sh_audio->wf->wFormatTag = sh_audio->format;
897 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ForcingOutputAudiofmtTag,
898 force_audiofmttag);
901 mux_a=muxer_new_stream(muxer,MUXER_TYPE_AUDIO);
903 mux_a->buffer_size=0x100000; //16384;
904 mux_a->buffer=malloc(mux_a->buffer_size);
905 if (!mux_a->buffer)
906 mencoder_exit(1,MSGTR_MemAllocFailed);
908 mux_a->source=sh_audio;
910 mux_a->codec=out_audio_codec;
912 ao_data.samplerate = force_srate;
913 ao_data.channels = 0;
914 ao_data.format = audio_output_format;
915 if(!init_audio_filters(sh_audio,
916 // input:
917 new_srate,
918 // output:
919 &ao_data.samplerate, &ao_data.channels, &ao_data.format)) {
920 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_AudioFilterChainPreinitError);
921 mencoder_exit(1, NULL);
924 aparams.channels = ao_data.channels;
925 aparams.sample_rate = ao_data.samplerate;
926 aparams.audio_preload = 1000 * audio_preload;
927 if(mux_a->codec != ACODEC_COPY) {
928 aencoder = new_audio_encoder(mux_a, &aparams);
929 if(!aencoder)
930 mencoder_exit(1, NULL);
931 if(!init_audio_filters(sh_audio,
932 new_srate,
933 &aparams.sample_rate, &aparams.channels, &aencoder->input_format)) {
934 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_NoMatchingFilter);
935 mencoder_exit(1,NULL);
938 switch(mux_a->codec){
939 case ACODEC_COPY:
940 if (playback_speed != 1.0) mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_NoSpeedWithFrameCopy);
941 if (sh_audio->format >= 0x10000) {
942 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantCopyAudioFormat, sh_audio->format);
943 mencoder_exit(1,NULL);
945 if (sh_audio->wf){
946 mux_a->wf=malloc(sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
947 memcpy(mux_a->wf, sh_audio->wf, sizeof(WAVEFORMATEX) + sh_audio->wf->cbSize);
948 if(!sh_audio->i_bps) sh_audio->i_bps=mux_a->wf->nAvgBytesPerSec;
949 } else {
950 mux_a->wf = malloc(sizeof(WAVEFORMATEX));
951 mux_a->wf->nBlockAlign = 1; //mux_a->h.dwSampleSize;
952 mux_a->wf->wFormatTag = sh_audio->format;
953 mux_a->wf->nChannels = sh_audio->channels;
954 mux_a->wf->nSamplesPerSec = sh_audio->samplerate;
955 mux_a->wf->nAvgBytesPerSec=sh_audio->i_bps; //mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
956 mux_a->wf->wBitsPerSample = 16; // FIXME
957 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
959 if(sh_audio->audio.dwScale){
960 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize;
961 mux_a->h.dwScale=sh_audio->audio.dwScale;
962 mux_a->h.dwRate=sh_audio->audio.dwRate;
963 } else {
964 mux_a->h.dwSampleSize=mux_a->wf->nBlockAlign;
965 mux_a->h.dwScale=mux_a->h.dwSampleSize;
966 mux_a->h.dwRate=mux_a->wf->nAvgBytesPerSec;
968 mux_a->h.dwRate *= playback_speed;
969 mux_a->wf->nSamplesPerSec *= playback_speed;
970 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
971 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
972 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
973 break;
976 if ( mp_msg_test(MSGT_MENCODER,MSGL_DBG2) ) print_wave_header(mux_a->wf, MSGL_DBG2);
978 if (! ignore_start)
979 muxer->audio_delay_fix += sh_audio->stream_delay;
981 } // if(sh_audio)
983 decoded_frameno=0;
985 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
986 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
987 signal(SIGTERM,exit_sighandler); // kill
988 signal(SIGHUP,exit_sighandler); // broken terminal line
989 signal(SIGPIPE,exit_sighandler); // broken pipe
991 timer_start=GetTimerMS();
992 } // if (!curfile) // if this was the first file.
993 else {
994 if (!mux_a != !sh_audio) {
995 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoAudioFileMismatch);
996 mencoder_exit(1,NULL);
998 if (sh_audio && mux_a->codec == ACODEC_COPY) {
999 if (playback_speed != 1.0) mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_NoSpeedWithFrameCopy);
1000 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
1001 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
1002 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
1003 if (sh_audio->wf) {
1004 if ((mux_a->wf->wFormatTag != sh_audio->wf->wFormatTag) ||
1005 (mux_a->wf->nChannels != sh_audio->wf->nChannels) ||
1006 (mux_a->wf->nSamplesPerSec != sh_audio->wf->nSamplesPerSec * playback_speed))
1008 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
1009 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * playback_speed),
1010 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1011 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_AudioCopyFileMismatch);
1012 mencoder_exit(1,NULL);
1014 } else {
1015 if ((mux_a->wf->wFormatTag != sh_audio->format) ||
1016 (mux_a->wf->nChannels != sh_audio->channels) ||
1017 (mux_a->wf->nSamplesPerSec != sh_audio->samplerate * playback_speed))
1019 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ACodecFramecopy,
1020 sh_audio->wf->wFormatTag, sh_audio->wf->nChannels, (int)(sh_audio->wf->nSamplesPerSec * playback_speed),
1021 sh_audio->wf->wBitsPerSample, sh_audio->wf->nAvgBytesPerSec, 0);
1022 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_AudioCopyFileMismatch);
1023 mencoder_exit(1,NULL);
1026 } else if (sh_audio) {
1027 int out_srate = mux_a->wf->nSamplesPerSec;
1028 int out_channels = mux_a->wf->nChannels;
1029 int out_format = aencoder->input_format;
1030 if (!init_audio_filters(sh_audio, new_srate,
1031 &out_srate, &out_channels,
1032 &out_format)) {
1033 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_NoMatchingFilter);
1034 mencoder_exit(1, NULL);
1036 mux_a->wf->nSamplesPerSec = out_srate;
1037 mux_a->wf->nChannels = out_channels;
1041 if (seek_to_sec) {
1042 demux_seek(demuxer, seek_to_sec, audio_delay, 1);
1043 // there is 2 way to handle the -ss option in 3-pass mode:
1044 // > 1. do the first pass for the whole file, and use -ss for 2nd/3rd pases only
1045 // > 2. do all the 3 passes with the same -ss value
1046 // this line enables behaviour 1. (and kills 2. at the same time):
1047 // if(demuxer2) demux_seek(demuxer2, d, audio_delay, 1);
1050 if (out_file_format == MUXER_TYPE_MPEG)
1052 if (audio_preload > 0.4) {
1053 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_LimitingAudioPreload);
1054 audio_preload = 0.4;
1056 if (audio_density < 4) {
1057 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_IncreasingAudioDensity);
1058 audio_density = 4;
1062 if(file_format == DEMUXER_TYPE_TV)
1064 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_ZeroingAudioPreloadAndMaxPtsCorrection);
1065 audio_preload = 0.0;
1066 default_max_pts_correction = 0;
1069 play_n_frames=play_n_frames_mf;
1070 if (curfile && end_at.type == END_AT_TIME) end_at.pos += mux_v->timer;
1072 if (edl_records) free_edl(edl_records);
1073 next_edl_record = edl_records = NULL;
1074 edl_muted = 0;
1075 edl_seeking = 1;
1076 if (edl_filename) {
1077 next_edl_record = edl_records = edl_parse_file();
1080 if (sh_audio && audio_delay != 0.) fixdelay(d_video, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1082 while(!at_eof){
1084 int blit_frame=0;
1085 float a_pts=0;
1086 float v_pts=0;
1087 int skip_flag=0; // 1=skip -1=duplicate
1089 if((end_at.type == END_AT_SIZE && end_at.pos <= stream_tell(muxer->stream)) ||
1090 (end_at.type == END_AT_TIME && end_at.pos < mux_v->timer))
1091 break;
1093 if(play_n_frames>=0){
1094 --play_n_frames;
1095 if(play_n_frames<0) break;
1097 if(dvd_last_chapter>0) {
1098 int cur_chapter = demuxer_get_current_chapter(demuxer);
1099 if(cur_chapter!=-1 && cur_chapter+1>dvd_last_chapter)
1100 break;
1103 goto_redo_edl:
1104 if (next_edl_record && sh_video && sh_video->pts >= next_edl_record->start_sec) {
1105 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1106 float last_pos = d_video->pts;
1107 int result;
1108 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_SKIP: start [%f], stop [%f], length [%f]\n",
1109 next_edl_record->start_sec, next_edl_record->stop_sec, next_edl_record->length_sec);
1111 result = edl_seek(next_edl_record, demuxer, d_audio, mux_a, &frame_data, mux_v->codec==VCODEC_COPY);
1113 if (result == 2) { at_eof=1; break; } // EOF
1114 else if (result == 0) edl_seeking = 0; // no seeking
1115 else { // sucess
1116 edl_muted = 0;
1117 if (last_pos >= sh_video->pts) {
1118 // backwards seek detected!! Forget about this EDL skip altogether.
1119 next_edl_record = next_edl_record->next;
1121 else for (next_edl_record = edl_records; next_edl_record; next_edl_record = next_edl_record->next) {
1122 /* note the use of stop_sec,
1123 meaning if by some magical way we landed in the MIDDLE of a censored area,
1124 in the next loop it will jump out of it.
1126 if (next_edl_record->stop_sec > sh_video->pts) break; // we got to the right place.
1127 if (next_edl_record->action == EDL_MUTE) edl_muted = !edl_muted; // toggle mute each time.
1130 /* for a pedantic EDL, that doesn't show even a single
1131 frame from the "censored" area, uncomment next line. */
1132 goto goto_redo_edl;
1134 } else if (next_edl_record->action == EDL_MUTE) {
1135 edl_muted = !edl_muted; // This variable does nothing for now.
1136 mp_msg(MSGT_CPLAYER, MSGL_DBG4, "EDL_MUTE: [%f]\n", next_edl_record->start_sec );
1137 next_edl_record=next_edl_record->next;
1142 if(sh_audio){
1143 // get audio:
1144 while(mux_a->timer-audio_preload<mux_v->timer){
1145 float tottime;
1146 int len=0;
1148 ptimer_start = GetTimerMS();
1149 // CBR - copy 0.5 sec of audio
1150 // or until the end of video:
1151 tottime = stop_time(demuxer, mux_v);
1152 if (tottime != -1) {
1153 tottime -= mux_a->timer;
1154 if (tottime > 1./audio_density) tottime = 1./audio_density;
1156 else tottime = 1./audio_density;
1158 // let's not output more audio than necessary
1159 if (tottime <= 0) break;
1161 if(aencoder)
1163 if(mux_a->h.dwSampleSize) /* CBR */
1165 if(aencoder->set_decoded_len)
1167 len = mux_a->h.dwSampleSize*(int)(mux_a->h.dwRate*tottime);
1168 aencoder->set_decoded_len(aencoder, len);
1170 else
1171 len = aencoder->decode_buffer_size;
1173 len = dec_audio(sh_audio, aencoder->decode_buffer, len);
1174 mux_a->buffer_len += aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len,
1175 aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1176 if(mux_a->buffer_len < mux_a->wf->nBlockAlign)
1177 len = 0;
1178 else
1179 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1181 else /* VBR */
1183 int sz = 0;
1184 while(1)
1186 len = 0;
1187 if(! sz)
1188 sz = aencoder->get_frame_size(aencoder);
1189 if(sz > 0 && mux_a->buffer_len >= sz)
1191 len = sz;
1192 break;
1194 len = dec_audio(sh_audio,aencoder->decode_buffer, aencoder->decode_buffer_size);
1195 if(len <= 0)
1197 len = 0;
1198 break;
1200 len = aencoder->encode(aencoder, mux_a->buffer + mux_a->buffer_len, aencoder->decode_buffer, len, mux_a->buffer_size-mux_a->buffer_len);
1201 mux_a->buffer_len += len;
1204 if (mux_v->timer == 0) mux_a->h.dwInitialFrames++;
1206 else {
1207 if(mux_a->h.dwSampleSize){
1208 switch(mux_a->codec){
1209 case ACODEC_COPY: // copy
1210 len=mux_a->wf->nAvgBytesPerSec*tottime;
1211 len/=mux_a->h.dwSampleSize;if(len<1) len=1;
1212 len*=mux_a->h.dwSampleSize;
1213 len=demux_read_data(sh_audio->ds,mux_a->buffer,len);
1214 break;
1216 } else {
1217 // VBR - encode/copy an audio frame
1218 switch(mux_a->codec){
1219 case ACODEC_COPY: // copy
1220 len=ds_get_packet(sh_audio->ds,(unsigned char**) &mux_a->buffer);
1221 break;
1225 if(len<=0) break; // EOF?
1226 muxer_write_chunk(mux_a,len,0x10, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1227 if(!mux_a->h.dwSampleSize && mux_a->timer>0)
1228 mux_a->wf->nAvgBytesPerSec=0.5f+(double)mux_a->size/mux_a->timer; // avg bps (VBR)
1229 if(mux_a->buffer_len>=len){
1230 mux_a->buffer_len-=len;
1231 fast_memcpy(mux_a->buffer,mux_a->buffer+len,mux_a->buffer_len);
1235 audiosamples++;
1236 audiorate+= (GetTimerMS() - ptimer_start);
1241 // get video frame!
1243 if (!frame_data.already_read) {
1244 frame_data.in_size=video_read_frame(sh_video,&frame_data.frame_time,&frame_data.start,force_fps);
1245 sh_video->timer+=frame_data.frame_time;
1247 frame_data.frame_time /= playback_speed;
1248 if(frame_data.in_size<0){ at_eof=1; break; }
1249 ++decoded_frameno;
1251 v_timer_corr-=frame_data.frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate;
1253 if(demuxer2){ // 3-pass encoding, read control file (frameno.avi)
1254 // find our frame:
1255 while(next_frameno<decoded_frameno){
1256 int* start;
1257 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1258 if(len<0){ at_eof=1;break;}
1259 if(len==0) --skip_flag; else // duplicate
1260 if(len==4) next_frameno=start[0];
1262 if(at_eof) break;
1263 // if(skip_flag) printf("!!!!!!!!!!!!\n");
1264 skip_flag=next_frameno-decoded_frameno;
1265 // find next frame:
1266 while(next_frameno<=decoded_frameno){
1267 int* start;
1268 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1269 if(len<0){ at_eof=1;break;}
1270 if(len==0) --skip_flag; else // duplicate
1271 if(len==4) next_frameno=start[0];
1273 // if(at_eof) break;
1274 // printf("Current fno=%d requested=%d skip=%d \n",decoded_frameno,fno,skip_flag);
1275 } else {
1277 // check frame duplicate/drop:
1279 //printf("\r### %5.3f ###\n",v_timer_corr);
1280 float mux_frametime = (float)mux_v->h.dwScale/mux_v->h.dwRate;
1282 if (v_timer_corr >= mux_frametime && (skip_limit<0 || skip_flag < skip_limit)) {
1283 v_timer_corr-=mux_frametime;
1284 ++skip_flag; // skip
1286 while (v_timer_corr <= -mux_frametime && (skip_limit<0 || -skip_flag < skip_limit)) {
1287 v_timer_corr+=mux_frametime;
1288 --skip_flag; // dup
1291 // either v_pts_corr is big, more than 2 times framerate, then we follow its advice,
1292 // or, it cancels out v_timer_corr, in which case be happy and do nothing.
1294 while ((v_pts_corr <= -mux_frametime && skip_flag > 0) || (v_pts_corr <= -2*mux_frametime)) {
1295 v_pts_corr+=mux_frametime;
1296 --skip_flag; // dup
1298 if ((v_pts_corr >= mux_frametime && skip_flag < 0) || (v_pts_corr >= 2*mux_frametime)) {
1299 if (skip_flag<=0) { // we can't skip more than 1 frame now
1300 v_pts_corr-=mux_frametime;
1301 ++skip_flag; // skip
1305 } // demuxer2
1307 ptimer_start = GetTimerMS();
1309 switch(mux_v->codec){
1310 case VCODEC_COPY:
1311 mux_v->buffer=frame_data.start;
1312 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);
1313 break;
1314 case VCODEC_FRAMENO:
1315 mux_v->buffer=(unsigned char *)&decoded_frameno; // tricky
1316 if(skip_flag<=0) muxer_write_chunk(mux_v,sizeof(int),0x10, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1317 break;
1318 default:
1319 // decode_video will callback down to ve_*.c encoders, through the video filters
1320 {void *decoded_frame = decode_video(sh_video,frame_data.start,frame_data.in_size,
1321 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);
1322 blit_frame = decoded_frame && filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE);}
1324 if (sh_video->vf_initialized < 0) mencoder_exit(1, NULL);
1326 if(!blit_frame){
1327 if (play_n_frames >= 0)
1328 play_n_frames++;
1329 badframes++;
1330 if(skip_flag<=0){
1331 // unwanted skipping of a frame, what to do?
1332 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1333 #if 0
1334 // Old code apparently made under the assumption that !blit_frame means
1335 // decoding failed due to corruption or something.. but duplication and
1336 // skipping of frames should be entirely disabled when skip_limit==0,
1337 // and must be in order for many filters to work with -noskip.
1338 // Eventually this entire block should probably be removed.
1339 if(skip_limit==0){
1340 // skipping not allowed -> write empty frame:
1341 if (!encode_duplicates || !sh_video->vfilter || ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1342 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1343 } else {
1344 // skipping allowed -> skip it and distriubute timer error:
1345 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1347 #endif
1352 videosamples++;
1353 videorate+=(GetTimerMS() - ptimer_start);
1355 if(skip_flag<0){
1356 // duplicate frame
1357 if(!quiet) mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_DuplicateFrames,-skip_flag);
1358 while(skip_flag<0){
1359 duplicatedframes++;
1360 if (!encode_duplicates || !sh_video->vfilter || ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter, VFCTRL_DUPLICATE_FRAME, 0) != CONTROL_TRUE)
1361 muxer_write_chunk(mux_v,0,0, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
1362 ++skip_flag;
1364 } else
1365 if(skip_flag>0){
1366 // skip frame
1367 if(!quiet) mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_SkipFrame);
1368 skippedframes++;
1369 --skip_flag;
1372 if(sh_audio && !demuxer2){
1373 float AV_delay,x;
1374 // A-V sync!
1375 #if 0
1376 if(pts_from_bps){
1377 unsigned int samples=(sh_audio->audio.dwSampleSize)?
1378 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
1379 (d_audio->block_no); // <- used for VBR audio
1380 // printf("samples=%d \n",samples);
1381 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
1382 delay_corrected=1;
1383 } else
1384 #endif
1386 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
1387 a_pts=d_audio->pts;
1388 if(!delay_corrected) if(a_pts) delay_corrected=1;
1389 //printf("*** %5.3f ***\n",a_pts);
1390 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1392 v_pts=sh_video ? sh_video->pts : d_video->pts;
1393 // av = compensated (with out buffering delay) A-V diff
1394 AV_delay=(a_pts-v_pts);
1395 AV_delay-=audio_delay;
1396 AV_delay /= playback_speed;
1397 AV_delay-=mux_a->timer-(mux_v->timer-(v_timer_corr+v_pts_corr));
1398 // adjust for encoder delays
1399 AV_delay -= (float) mux_a->encoder_delay * mux_a->h.dwScale/mux_a->h.dwRate;
1400 AV_delay += (float) mux_v->encoder_delay * mux_v->h.dwScale/mux_v->h.dwRate;
1401 // compensate input video timer by av:
1402 x=AV_delay*0.1f;
1403 if(x<-max_pts_correction) x=-max_pts_correction; else
1404 if(x> max_pts_correction) x= max_pts_correction;
1405 if(default_max_pts_correction>=0)
1406 max_pts_correction=default_max_pts_correction*playback_speed;
1407 else
1408 max_pts_correction=sh_video->frametime*0.10 *playback_speed; // +-10% of time
1409 // sh_video->timer-=x;
1410 c_total+=x;
1411 v_pts_corr+=x;
1414 // printf("A:%6.1f V:%6.1f A-V:%7.3f oAV:%7.3f diff:%7.3f ct:%7.3f vpc:%7.3f \r",
1415 // a_pts,v_pts,a_pts-v_pts,
1416 // (float)(mux_a->timer-mux_v->timer),
1417 // AV_delay, c_total, v_pts_corr );
1418 // printf("V:%6.1f \r", d_video->pts );
1420 #if 0
1421 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",
1422 a_pts,v_pts,a_pts-v_pts,c_total,
1423 (int)sh_video->num_frames,(int)sh_video->num_frames_decoded,
1424 (sh_video->timer>0.5)?(int)(100.0*video_time_usage/(double)sh_video->timer):0,
1425 (sh_video->timer>0.5)?(int)(100.0*vout_time_usage/(double)sh_video->timer):0,
1426 (sh_video->timer>0.5)?(100.0*audio_time_usage/(double)sh_video->timer):0
1427 ,cache_fill_status
1429 #endif
1431 { float t=(GetTimerMS()-timer_start)*0.001f;
1432 float len=(demuxer->movi_end-demuxer->movi_start);
1433 off_t pos = demuxer->filepos >= 0 ? demuxer->filepos : stream_tell(demuxer->stream);
1434 float p=len>1000 ? (float)(pos-demuxer->movi_start) / len :
1435 (demuxer_get_percent_pos(demuxer) / 100.0);
1436 #if 0
1437 if(!len && sh_audio && sh_audio->audio.dwLength>100){
1438 p=(sh_audio->audio.dwSampleSize? ds_tell(sh_audio->ds)/sh_audio->audio.dwSampleSize : sh_audio->ds->block_no)
1439 / (float)(sh_audio->audio.dwLength);
1441 #endif
1442 #if 0
1443 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"%d < %d < %d \r",
1444 (int)demuxer->movi_start,
1445 (int)demuxer->filepos,
1446 (int)demuxer->movi_end);
1447 #else
1448 if(!quiet) {
1449 if( mp_msg_test(MSGT_STATUSLINE,MSGL_V) ) {
1450 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",
1451 mux_v->timer, decoded_frameno, (int)(p*100),
1452 (t>1) ? (int)(decoded_frameno/t+0.5) : 0,
1453 (p>0.001) ? (int)((t/p-t)/60) : 0,
1454 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1455 v_pts_corr,
1456 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1457 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0,
1458 audiorate/audiosamples, videorate/videosamples,
1459 duplicatedframes, badframes, skippedframes
1461 } else
1462 mp_msg(MSGT_STATUSLINE,MSGL_STATUS,"Pos:%6.1fs %6df (%2d%%) %5.2ffps Trem:%4dmin %3dmb A-V:%5.3f [%d:%d]\r",
1463 mux_v->timer, decoded_frameno, (int)(p*100),
1464 (t>1) ? (float)(decoded_frameno/t) : 0,
1465 (p>0.001) ? (int)((t/p-t)/60) : 0,
1466 (p>0.001) ? (int)(stream_tell(muxer->stream)/p/1024/1024) : 0,
1467 v_pts_corr,
1468 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1469 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0
1472 #endif
1474 fflush(stdout);
1476 #ifdef CONFIG_DVDREAD
1477 // DVD sub:
1478 if(vobsub_writer){
1479 unsigned char* packet=NULL;
1480 int len;
1481 while((len=ds_get_packet_sub(d_dvdsub,&packet))>0){
1482 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);
1483 vobsub_out_output(vobsub_writer,packet,len,mux_v->timer + d_dvdsub->pts - sh_video->pts);
1486 else
1487 #endif
1488 update_subtitles(sh_video, sh_video->pts, d_dvdsub, 0);
1490 frame_data = (s_frame_data){ .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
1492 #if 0
1493 if(ferror(muxer_f)) {
1494 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile, out_filename);
1495 mencoder_exit(1, NULL);
1497 #endif
1499 } // while(!at_eof)
1501 if (!interrupted && filelist[++curfile].name != 0) {
1502 if (sh_video && sh_video->vfilter) { // Before uniniting sh_video and the filter chain, break apart the VE.
1503 vf_instance_t * ve; // this will be the filter right before the ve.
1504 for (ve = sh_video->vfilter; ve->next && ve->next->next; ve = ve->next);
1506 if (ve->next) ve->next = NULL; // I'm telling the last filter, before the VE, there is nothing after it
1507 else sh_video->vfilter = NULL; // There is no chain except the VE.
1510 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1511 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1512 if(demuxer) free_demuxer(demuxer);
1513 if(stream) free_stream(stream); // kill cache thread
1515 at_eof = 0;
1517 m_config_pop(mconfig);
1518 goto play_next_file;
1521 /* Emit the remaining frames in the video system */
1522 /*TODO emit frmaes delayed by decoder lag*/
1523 if(sh_video && sh_video->vfilter){
1524 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_FlushingVideoFrames);
1525 if (!((vf_instance_t *)sh_video->vfilter)->fmt.have_configured)
1526 mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_FiltersHaveNotBeenConfiguredEmptyFile);
1527 else
1528 ((vf_instance_t *)sh_video->vfilter)->control(sh_video->vfilter,
1529 VFCTRL_FLUSH_FRAMES, 0);
1532 if(aencoder)
1533 if(aencoder->fixup)
1534 aencoder->fixup(aencoder);
1536 if (muxer->cont_write_index) muxer_write_index(muxer);
1537 muxer_f_size=stream_tell(muxer->stream);
1538 stream_seek(muxer->stream,0);
1539 if (muxer->cont_write_header) muxer_write_header(muxer); // update header
1540 #if 0
1541 if(ferror(muxer_f) || fclose(muxer_f) != 0) {
1542 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile, out_filename);
1543 mencoder_exit(1, NULL);
1545 #endif
1546 if(vobsub_writer)
1547 vobsub_out_close(vobsub_writer);
1549 if(out_video_codec==VCODEC_FRAMENO && mux_v->timer>100){
1550 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"650MB",(int)((650*1024*1024-muxer_f_size)/mux_v->timer/125));
1551 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"700MB",(int)((700*1024*1024-muxer_f_size)/mux_v->timer/125));
1552 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"800MB",(int)((800*1024*1024-muxer_f_size)/mux_v->timer/125));
1553 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"2 x 650MB",(int)((2*650*1024*1024-muxer_f_size)/mux_v->timer/125));
1554 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"2 x 700MB",(int)((2*700*1024*1024-muxer_f_size)/mux_v->timer/125));
1555 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_RecommendedVideoBitrate,"2 x 800MB",(int)((2*800*1024*1024-muxer_f_size)/mux_v->timer/125));
1558 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VideoStreamResult,
1559 (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);
1560 if(sh_audio)
1561 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_AudioStreamResult,
1562 (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);
1564 if(sh_audio){ uninit_audio(sh_audio);sh_audio=NULL; }
1565 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1566 if(demuxer) free_demuxer(demuxer);
1567 if(stream) free_stream(stream); // kill cache thread
1569 return interrupted;
1573 #if 0
1574 /* Flip the image in src and store the result in dst. src and dst may overlap.
1575 width is the size of each line in bytes. */
1576 static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width,
1577 int height)
1579 uint8_t* tmp = malloc(width);
1580 int i;
1582 for(i = 0; i < height/2; i++) {
1583 fast_memcpy(tmp, &src[i*width], width);
1584 fast_memcpy(&dst[i * width], &src[(height - i) * width], width);
1585 fast_memcpy(&dst[(height - i) * width], tmp, width);
1588 free(tmp);
1589 return dst;
1591 #endif
1594 static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v) {
1595 float timeleft = -1;
1596 if (play_n_frames >= 0) timeleft = mux_v->timer + play_n_frames * (double)(mux_v->h.dwScale) / mux_v->h.dwRate;
1597 if (end_at.type == END_AT_TIME && (timeleft > end_at.pos || timeleft == -1)) timeleft = end_at.pos;
1598 if (next_edl_record && demuxer && demuxer->video) { // everything is OK to be checked
1599 float tmp = mux_v->timer + next_edl_record->start_sec - demuxer->video->pts;
1600 if (timeleft == -1 || timeleft > tmp) {
1601 // There's less time in EDL than what we already know
1602 if (next_edl_record->action == EDL_SKIP && edl_seeking) {
1603 timeleft = tmp;
1604 } else if (next_edl_record->action == EDL_MUTE) {
1605 //timeleft = next_edl_record->start_sec - demuxer->video->pts;
1606 // For the moment (and probably forever) EDL mute doesn't work in MEncoder
1610 return timeleft;
1613 static float calc_a_pts(demux_stream_t *d_audio) {
1614 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
1615 float a_pts = 0.;
1616 if (sh_audio)
1617 a_pts = d_audio->pts + (ds_tell_pts(d_audio) - sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1618 return a_pts;
1621 static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a) {
1622 sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
1623 int samplesize, avg;
1624 float a_pts = calc_a_pts(d_audio);
1626 if (!sh_audio) return a_pts;
1628 if (sh_audio->audio.dwScale) samplesize = sh_audio->audio.dwSampleSize;
1629 else samplesize = (sh_audio->wf ? sh_audio->wf->nBlockAlign : 1);
1630 avg = (sh_audio->wf ? sh_audio->wf->nAvgBytesPerSec : sh_audio->i_bps);
1632 // after a demux_seek, a_pts will be zero until you read some audio.
1633 // carefully checking if a_pts is truely correct by reading tiniest amount of data possible.
1634 if (pts > a_pts && a_pts == 0.0 && samplesize) {
1635 if (demux_read_data(sh_audio->ds,mux_a->buffer,samplesize) <= 0) return a_pts; // EOF
1636 a_pts = calc_a_pts(d_audio);
1639 while (pts > a_pts) {
1640 int len;
1641 if (samplesize) {
1642 len = avg * (pts - a_pts > 0.5 ? 0.5 : pts - a_pts);
1643 len/= samplesize; if(len<1) len=1;
1644 len*= samplesize;
1645 len = demux_read_data(sh_audio->ds,mux_a->buffer,len);
1646 } else {
1647 unsigned char * crap;
1648 len = ds_get_packet(sh_audio->ds, &crap);
1650 if (len <= 0) break; // EOF of audio.
1651 a_pts = calc_a_pts(d_audio);
1653 return a_pts;
1656 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) {
1657 sh_video_t * sh_video = d_video->sh;
1658 vf_instance_t * vfilter = sh_video ? sh_video->vfilter : NULL;
1659 int done = 0;
1661 while (!interrupted) {
1662 float a_pts = 0.;
1664 if (!frame_data->already_read) { // when called after fixdelay, a frame is already read
1665 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
1666 if(frame_data->in_size<0) return 2;
1667 sh_video->timer += frame_data->frame_time;
1669 frame_data->already_read = 0;
1671 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
1673 if (done) {
1674 // wait for keyframe in case of -ovc copy
1675 if (!framecopy || (sh_video->ds->flags & 1)) {
1676 frame_data->already_read = 1;
1677 return 1;
1680 if (sh_video->pts >= end_pts) done = 1;
1682 if (vfilter) {
1683 int softskip = (vfilter->control(vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) == CONTROL_TRUE);
1684 void *decoded_frame = decode_video(sh_video, frame_data->start, frame_data->in_size, !softskip, MP_NOPTS_VALUE);
1685 if (decoded_frame)
1686 filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE);
1689 if (print_info) mp_msg(MSGT_MENCODER, MSGL_STATUS,
1690 MSGTR_EdlSkipStartEndCurrent,
1691 next_edl_record->start_sec, next_edl_record->stop_sec,
1692 sh_video->pts, a_pts);
1694 if (interrupted) return 2;
1695 return 1;
1698 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) {
1699 // TODO: Find a way to encode silence instead of deleting video
1700 sh_video_t * sh_video = d_video->sh;
1701 float a_pts;
1703 // demux_seek has a weirdness that sh_video->pts is meaningless,
1704 // until a single frame is read... Same for audio actually too.
1705 // Reading one frame, and keeping it.
1706 frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
1707 if(frame_data->in_size<0) return;
1708 sh_video->timer += frame_data->frame_time;
1709 frame_data->already_read = 1;
1711 a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
1713 if (audio_delay > 0) return;
1714 else if (sh_video->pts - frame_data->frame_time + audio_delay >= a_pts) return;
1716 slowseek(a_pts - audio_delay, d_video, d_audio, mux_a, frame_data, framecopy, 0);
1719 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) {
1720 sh_video_t * sh_video = demuxer->video ? demuxer->video->sh : NULL;
1722 if (!sh_video) return 0;
1723 if (sh_video->pts >= next_edl_record->stop_sec) return 1; // nothing to do...
1725 if (!edl_seek_type) {
1726 if(demux_seek(demuxer, next_edl_record->stop_sec - sh_video->pts, audio_delay, 0)){
1727 sh_video->pts = demuxer->video->pts;
1728 //if (vo_vobsub) vobsub_seek(vo_vobsub,sh_video->pts);
1729 //if(vo_spudec) spudec_reset(vo_spudec);
1730 if (audio_delay != 0.0) fixdelay(demuxer->video, d_audio, mux_a, frame_data, framecopy);
1731 return 1;
1733 // non-seekable stream.
1734 return 0;
1737 // slow seek, read every frame.
1739 return slowseek(next_edl_record->stop_sec, demuxer->video, d_audio, mux_a, frame_data, framecopy, 1);