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