2 #define VCODEC_FRAMENO 1
5 #define VCODEC_LIBAVCODEC 4
9 #define VCODEC_QTVIDEO 10
12 #define VCODEC_X264 13
16 #define ACODEC_VBRMP3 2
19 #define ACODEC_TOOLAME 5
21 #define ACODEC_TWOLAME 7
43 #include "cpudetect.h"
45 #include "codec-cfg.h"
48 #include "parser-mecmd.h"
50 #include "libmpdemux/stream.h"
51 #include "libmpdemux/demuxer.h"
52 #include "libmpdemux/stheader.h"
53 #include "libmpdemux/mp3_hdr.h"
54 #include "libmpdemux/muxer.h"
57 #include "libvo/video_out.h"
59 #include "libaf/af_format.h"
61 #include "libmpcodecs/mp_image.h"
62 #include "libmpcodecs/dec_audio.h"
63 #include "libmpcodecs/dec_video.h"
64 #include "libmpcodecs/vf.h"
66 // for MPEGLAYER3WAVEFORMAT:
67 #include "libmpdemux/ms_hdr.h"
71 #include "libvo/fastmemcpy.h"
73 #include "osdep/timer.h"
78 #ifdef USE_LIBAVCODEC_SO
79 #include <ffmpeg/avcodec.h>
81 #include "libavcodec/avcodec.h"
85 #include "libmpcodecs/ae.h"
86 int vo_doublebuffering
=0;
87 int vo_directrendering
=0;
88 int vo_config_count
=0;
89 int forced_subs_only
=0;
91 //--------------------------
94 int stream_cache_size
=-1;
95 #ifdef USE_STREAM_CACHE
96 extern int cache_fill_status
;
98 float stream_cache_min_percent
=20.0;
99 float stream_cache_prefill_percent
=5.0;
101 #define cache_fill_status 0
108 char* audio_lang
=NULL
;
109 char* dvdsub_lang
=NULL
;
110 static char* spudec_ifo
=NULL
;
112 static char** audio_codec_list
=NULL
; // override audio codec
113 static char** video_codec_list
=NULL
; // override video codec
114 static char** audio_fm_list
=NULL
; // override audio codec family
115 static char** video_fm_list
=NULL
; // override video codec family
117 static int out_audio_codec
=-1;
118 static int out_video_codec
=-1;
120 int out_file_format
=MUXER_TYPE_AVI
; // default to AVI
122 // audio stream skip/resync functions requires only for seeking.
123 // (they should be implemented in the audio codec layer)
124 //void skip_audio_frame(sh_audio_t *sh_audio){}
125 //void resync_audio_stream(sh_audio_t *sh_audio){}
127 int verbose
=0; // must be global!
130 double video_time_usage
=0;
131 double vout_time_usage
=0;
132 double max_video_time_usage
=0;
133 double max_vout_time_usage
=0;
134 double cur_video_time_usage
=0;
135 double cur_vout_time_usage
=0;
139 char * proc_priority
=NULL
;
143 int delay_corrected
=1;
144 static float default_max_pts_correction
=-1;//0.01f;
145 static float max_pts_correction
=0;//default_max_pts_correction;
146 static float c_total
=0;
148 static float audio_preload
=0.5;
149 static float audio_delay
=0.0;
150 static int audio_density
=2;
153 static float force_ofps
=0; // set to 24 for inverse telecine
154 static int skip_limit
=-1;
155 float playback_speed
=1.0;
157 static int force_srate
=0;
158 static int audio_output_format
=0;
160 char *vobsub_out
=NULL
;
161 unsigned int vobsub_out_index
=0;
162 char *vobsub_out_id
=NULL
;
164 char* out_filename
="test.avi";
166 char *force_fourcc
=NULL
;
168 char* passtmpfile
="divx2pass.log";
170 static int play_n_frames
=-1;
171 static int play_n_frames_mf
=-1;
173 #include "libvo/font_load.h"
174 #include "libvo/sub.h"
177 char *font_name
=NULL
;
178 #ifdef HAVE_FONTCONFIG
179 extern int font_fontconfig
;
181 float font_factor
=0.75;
182 char **sub_name
=NULL
;
187 int suboverlap_enabled
= 1;
190 static sub_data
* subdata
=NULL
;
191 float sub_last_pts
= -303;
195 int encode_duplicates
=1;
197 // infos are empty by default
198 char *info_name
=NULL
;
199 char *info_artist
=NULL
;
200 char *info_genre
=NULL
;
201 char *info_subject
=NULL
;
202 char *info_copyright
=NULL
;
203 char *info_sourceform
=NULL
;
204 char *info_comment
=NULL
;
208 //char *out_audio_codec=NULL; // override audio codec
209 //char *out_video_codec=NULL; // override video codec
211 //#include "libmpeg2/mpeg2.h"
212 //#include "libmpeg2/mpeg2_internal.h"
214 //static int vo_w=0, vo_h=0;
216 //-------------------------- config stuff:
220 extern int m_config_parse_config_file(m_config_t
* config
, char *conffile
);
222 static int cfg_inc_verbose(m_option_t
*conf
){ ++verbose
; return 0;}
224 static int cfg_include(m_option_t
*conf
, char *filename
){
225 return m_config_parse_config_file(mconfig
, filename
);
228 static char *seek_to_sec
=NULL
;
229 static off_t seek_to_byte
=0;
231 static void parse_end_at();
232 static char * end_at_string
=0;
233 //static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width, int height);
237 static edl_record_ptr edl_records
= NULL
; ///< EDL entries memory area
238 static edl_record_ptr next_edl_record
= NULL
; ///< only for traversing edl_records
239 static short edl_muted
; ///< Stores whether EDL is currently in muted mode.
240 static short edl_seeking
; ///< When non-zero, stream is seekable.
241 static short edl_seek_type
; ///< When non-zero, frames are discarded instead of seeking.
242 static short edl_skip
; ///< -1 OR the value of in_size of an already read frame.
243 /** \brief Seeks for EDL
244 \return 1 for success, 0 for failure, 2 for EOF.
246 int edl_seek(edl_record_ptr next_edl_record
, demuxer_t
* demuxer
, demux_stream_t
*d_audio
, muxer_stream_t
* mux_a
, float * frame_time
, unsigned char ** start
, int framecopy
);
249 #include "cfg-mplayer-def.h"
250 #include "cfg-mencoder.h"
257 #include "libao2/audio_out.h"
259 static void mencoder_exit(int level
, char *how
)
262 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_ExitingHow
, mp_gettext(how
));
264 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_Exiting
);
269 void parse_cfgfiles( m_config_t
* conf
)
272 if ((conffile
= get_path("mencoder")) == NULL
) {
273 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_GetpathProblem
);
275 if (m_config_parse_config_file(conf
, conffile
) < 0)
276 mencoder_exit(1,MSGTR_ConfigfileError
);
282 //---------------------------------------------------------------------------
284 static int dec_audio(sh_audio_t
*sh_audio
,unsigned char* buffer
,int total
){
287 while(size
<total
&& !at_eof
){
289 if(len
>MAX_OUTBURST
) len
=MAX_OUTBURST
;
290 if(len
>sh_audio
->a_out_buffer_size
) len
=sh_audio
->a_out_buffer_size
;
291 if(len
>sh_audio
->a_out_buffer_len
){
292 int ret
=decode_audio(sh_audio
,
293 &sh_audio
->a_out_buffer
[sh_audio
->a_out_buffer_len
],
294 len
-sh_audio
->a_out_buffer_len
,
295 sh_audio
->a_out_buffer_size
-sh_audio
->a_out_buffer_len
);
296 if(ret
>0) sh_audio
->a_out_buffer_len
+=ret
; else at_eof
=1;
298 if(len
>sh_audio
->a_out_buffer_len
) len
=sh_audio
->a_out_buffer_len
;
299 memcpy(buffer
+size
,sh_audio
->a_out_buffer
,len
);
300 sh_audio
->a_out_buffer_len
-=len
; size
+=len
;
301 if(sh_audio
->a_out_buffer_len
>0)
302 memcpy(sh_audio
->a_out_buffer
,&sh_audio
->a_out_buffer
[len
],sh_audio
->a_out_buffer_len
);
307 //---------------------------------------------------------------------------
309 // this function returns the absoloute time for which MEncoder will switch files or move in the file.
310 // so audio can be cut correctly. -1 if there is no limit.
311 static float stop_time(demuxer_t
* demuxer
, muxer_stream_t
* mux_v
);
314 static int interrupted
=0;
316 enum end_at_type_t
{END_AT_NONE
, END_AT_TIME
, END_AT_SIZE
};
317 static enum end_at_type_t end_at_type
= END_AT_NONE
;
318 static double end_at
;
320 static void exit_sighandler(int x
){
322 interrupted
=2; /* 1 means error */
325 static muxer_t
* muxer
=NULL
;
326 static FILE* muxer_f
=NULL
;
328 extern void print_wave_header(WAVEFORMATEX
*h
);
330 int main(int argc
,char* argv
[]){
332 stream_t
* stream
=NULL
;
333 demuxer_t
* demuxer
=NULL
;
334 stream_t
* stream2
=NULL
;
335 demuxer_t
* demuxer2
=NULL
;
336 demux_stream_t
*d_audio
=NULL
;
337 demux_stream_t
*d_video
=NULL
;
338 demux_stream_t
*d_dvdsub
=NULL
;
339 sh_audio_t
*sh_audio
=NULL
;
340 sh_video_t
*sh_video
=NULL
;
341 int file_format
=DEMUXER_TYPE_UNKNOWN
;
342 int i
=DEMUXER_TYPE_UNKNOWN
;
343 void *vobsub_writer
=NULL
;
345 uint32_t ptimer_start
;
346 uint32_t audiorate
=0;
347 uint32_t videorate
=0;
348 uint32_t audiosamples
=1;
349 uint32_t videosamples
=1;
350 uint32_t skippedframes
=0;
351 uint32_t duplicatedframes
=0;
352 uint32_t badframes
=0;
354 muxer_stream_t
* mux_a
=NULL
;
355 muxer_stream_t
* mux_v
=NULL
;
356 off_t muxer_f_size
=0;
359 double v_timer_corr
=0;
361 m_entry_t
* filelist
= NULL
;
363 char* frameno_filename
="frameno.avi";
365 int decoded_frameno
=0;
370 unsigned int timer_start
;
371 ao_data_t ao_data
= {0,0,0,0,OUTBURST
,-1,0};
373 audio_encoding_params_t aparams
;
374 audio_encoder_t
*aencoder
= NULL
;
377 mp_msg_set_level(MSGL_STATUS
);
378 mp_msg(MSGT_CPLAYER
,MSGL_INFO
, "MEncoder " VERSION
" (C) 2000-2005 MPlayer Team\n");
380 /* Test for cpu capabilities (and corresponding OS support) for optimizing */
381 GetCpuCaps(&gCpuCaps
);
382 #if defined(ARCH_X86) || defined(ARCH_X86_64)
383 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"CPUflags: Type: %d MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
384 gCpuCaps
.cpuType
,gCpuCaps
.hasMMX
,gCpuCaps
.hasMMX2
,
385 gCpuCaps
.has3DNow
, gCpuCaps
.has3DNowExt
,
386 gCpuCaps
.hasSSE
, gCpuCaps
.hasSSE2
);
387 #ifdef RUNTIME_CPUDETECT
388 mp_msg(MSGT_CPLAYER
,MSGL_INFO
, MSGTR_CompiledWithRuntimeDetection
);
390 mp_msg(MSGT_CPLAYER
,MSGL_INFO
, MSGTR_CompiledWithCPUExtensions
);
392 mp_msg(MSGT_CPLAYER
,MSGL_INFO
," MMX");
395 mp_msg(MSGT_CPLAYER
,MSGL_INFO
," MMX2");
398 mp_msg(MSGT_CPLAYER
,MSGL_INFO
," 3DNow");
401 mp_msg(MSGT_CPLAYER
,MSGL_INFO
," 3DNowEx");
404 mp_msg(MSGT_CPLAYER
,MSGL_INFO
," SSE");
407 mp_msg(MSGT_CPLAYER
,MSGL_INFO
," SSE2");
409 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"\n\n");
416 if(!codecs_file
|| !parse_codec_cfg(codecs_file
)){
417 if(!parse_codec_cfg(get_path("codecs.conf"))){
418 if(!parse_codec_cfg(MPLAYER_CONFDIR
"/codecs.conf")){
419 if(!parse_codec_cfg(NULL
)){
420 mp_msg(MSGT_MENCODER
,MSGL_HINT
,MSGTR_CopyCodecsConf
);
421 mencoder_exit(1,NULL
);
423 mp_msg(MSGT_MENCODER
,MSGL_V
,MSGTR_BuiltinCodecsConf
);
428 // FIXME: get rid of -dvd and other tricky options
429 stream2
=open_stream(frameno_filename
,0,&i
);
431 demuxer2
=demux_open(stream2
,DEMUXER_TYPE_AVI
,-1,-1,-2,NULL
);
432 if(demuxer2
) mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_UsingPass3ControllFile
, frameno_filename
);
433 else mp_msg(MSGT_DEMUXER
,MSGL_ERR
,MSGTR_FormatNotRecognized
);
436 mconfig
= m_config_new();
437 m_config_register_options(mconfig
,mencoder_opts
);
438 parse_cfgfiles(mconfig
);
439 filelist
= m_config_parse_me_command_line(mconfig
, argc
, argv
);
440 if(!filelist
) mencoder_exit(1, MSGTR_ErrorParsingCommandLine
);
442 mp_msg_set_level(verbose
+MSGL_STATUS
);
447 for(i
=0; priority_presets_defs
[i
].name
; i
++){
448 if(strcasecmp(priority_presets_defs
[i
].name
, proc_priority
) == 0)
451 mp_msg(MSGT_CPLAYER
,MSGL_STATUS
,"Setting process priority: %s\n",
452 priority_presets_defs
[i
].name
);
453 SetPriorityClass(GetCurrentProcess(), priority_presets_defs
[i
].prio
);
462 #ifdef HAVE_FONTCONFIG
467 vo_font
=read_font_desc(font_name
,font_factor
,verbose
>1);
468 if(!vo_font
) mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_CantLoadFont
,font_name
);
471 vo_font
=read_font_desc(get_path("font/font.desc"),font_factor
,verbose
>1);
473 vo_font
=read_font_desc(MPLAYER_DATADIR
"/font/font.desc",font_factor
,verbose
>1);
475 #ifdef HAVE_FONTCONFIG
483 m_config_push(mconfig
);
484 m_entry_set_options(mconfig
,&filelist
[curfile
]);
485 filename
= filelist
[curfile
].name
;
488 mp_msg(MSGT_CPLAYER
, MSGL_FATAL
, MSGTR_MissingFilename
);
489 mencoder_exit(1,NULL
);
491 stream
=open_stream(filename
,0,&file_format
);
494 mp_msg(MSGT_CPLAYER
, MSGL_FATAL
, MSGTR_CannotOpenFile_Device
);
495 mencoder_exit(1,NULL
);
498 mp_msg(MSGT_CPLAYER
, MSGL_INFO
, MSGTR_OpenedStream
, file_format
, (int)(stream
->start_pos
), (int)(stream
->end_pos
));
501 if(stream
->type
==STREAMTYPE_DVD
){
502 if(audio_lang
&& audio_id
==-1) audio_id
=dvd_aid_from_lang(stream
,audio_lang
);
503 if(dvdsub_lang
&& dvdsub_id
==-1) dvdsub_id
=dvd_sid_from_lang(stream
,dvdsub_lang
);
507 stream
->start_pos
+=seek_to_byte
;
509 if(stream_cache_size
>0) stream_enable_cache(stream
,stream_cache_size
*1024,0,0);
511 if(demuxer2
) audio_id
=-2; /* do NOT read audio packets... */
513 //demuxer=demux_open(stream,file_format,video_id,audio_id,dvdsub_id);
514 demuxer
=demux_open(stream
,file_format
,audio_id
,video_id
,dvdsub_id
,filename
);
516 mp_msg(MSGT_DEMUXER
, MSGL_FATAL
, MSGTR_FormatNotRecognized
);
517 mp_msg(MSGT_DEMUXER
, MSGL_FATAL
, MSGTR_CannotOpenDemuxer
);
518 mencoder_exit(1,NULL
);
521 d_audio
=demuxer2
? demuxer2
->audio
: demuxer
->audio
;
522 d_video
=demuxer
->video
;
523 d_dvdsub
=demuxer
->sub
;
524 sh_audio
=d_audio
->sh
;
525 sh_video
=d_video
->sh
;
529 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_VideoStreamRequired
);
530 mencoder_exit(1,NULL
);
533 if(!video_read_properties(sh_video
)){
534 mp_msg(MSGT_CPLAYER
, MSGL_FATAL
, MSGTR_CannotReadVideoProperties
);
535 mencoder_exit(1,NULL
);
538 mp_msg(MSGT_MENCODER
,MSGL_INFO
,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
539 demuxer
->file_format
,sh_video
->format
, sh_video
->disp_w
,sh_video
->disp_h
,
540 sh_video
->fps
,sh_video
->frametime
544 sh_video
->fps
=force_fps
;
545 sh_video
->frametime
=1.0f
/sh_video
->fps
;
546 mp_msg(MSGT_MENCODER
,MSGL_INFO
,MSGTR_ForcingInputFPS
, sh_video
->fps
);
549 if(sh_audio
&& out_file_format
==MUXER_TYPE_RAWVIDEO
){
550 mp_msg(MSGT_MENCODER
,MSGL_ERR
,MSGTR_RawvideoDoesNotSupportAudio
);
553 if(sh_audio
&& out_audio_codec
<0){
555 mp_msg(MSGT_MENCODER
,MSGL_ERR
,MSGTR_DemuxerDoesntSupportNosound
);
556 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_NoAudioEncoderSelected
);
557 mencoder_exit(1,NULL
);
559 if(sh_video
&& out_video_codec
<0){
560 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_NoVideoEncoderSelected
);
561 mencoder_exit(1,NULL
);
564 if(sh_audio
&& (out_audio_codec
|| seek_to_sec
|| !sh_audio
->wf
|| playback_speed
!= 1.0)){
565 // Go through the codec.conf and find the best codec...
566 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
567 if(!init_best_audio_codec(sh_audio
,audio_codec_list
,audio_fm_list
)){
568 sh_audio
=d_audio
->sh
=NULL
; // failed to init :(
570 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
574 new_srate
= sh_audio
->samplerate
;
575 if (playback_speed
!= 1.0) {
576 new_srate
*= playback_speed
;
577 // limits are taken from libaf/af_resample.c
578 if (new_srate
< 8000) new_srate
= 8000;
579 if (new_srate
> 192000) new_srate
= 192000;
580 playback_speed
= (float)new_srate
/ (float)sh_audio
->samplerate
;
585 // after reading video params we should load subtitles because
586 // we know fps so now we can adjust subtitles time to ~6 seconds AST
588 // current_module="read_subtitles_file";
589 if(sub_name
&& sub_name
[0]){
590 subdata
=sub_read_file(sub_name
[0], sh_video
->fps
);
591 if(!subdata
) mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_CantLoadSub
,sub_name
[0]);
593 if(sub_auto
) { // auto load sub file ...
597 char *psub
= get_path( "sub/" );
598 tmp
= sub_filenames((psub
? psub
: ""), filename
);
601 subdata
=sub_read_file(tmp
? tmp
[0] : "default.sub", sh_video
->fps
);
602 while (tmp
&& tmp
[i
])
608 // set up video encoder:
610 if (!curfile
) { // curfile is non zero when a second file is opened
612 unsigned int palette
[16], width
, height
;
613 unsigned char tmp
[3] = { 0, 0, 0 };
614 if (spudec_ifo
&& vobsub_parse_ifo(NULL
,spudec_ifo
, palette
, &width
, &height
, 1, dvdsub_id
, tmp
) >= 0)
615 vobsub_writer
= vobsub_out_open(vobsub_out
, palette
, sh_video
->disp_w
, sh_video
->disp_h
,
616 vobsub_out_id
?vobsub_out_id
:(char *)tmp
, vobsub_out_index
);
618 if (vobsub_writer
== NULL
) {
620 if (vobsub_out_id
== NULL
&& stream
->type
== STREAMTYPE_DVD
) {
622 dvd_priv_t
*dvd
= (dvd_priv_t
*)stream
->priv
;
623 for (i
= 0; i
< dvd
->nr_of_subtitles
; ++i
)
624 if (dvd
->subtitles
[i
].id
== dvdsub_id
) {
625 tmp
[0] = (dvd
->subtitles
[i
].language
>> 8) & 0xff;
626 tmp
[1] = dvd
->subtitles
[i
].language
& 0xff;
632 vobsub_writer
=vobsub_out_open(vobsub_out
, stream
->type
==STREAMTYPE_DVD
?((dvd_priv_t
*)(stream
->priv
))->cur_pgc
->palette
:NULL
,
633 sh_video
->disp_w
, sh_video
->disp_h
, vobsub_out_id
, vobsub_out_index
);
639 unsigned int palette
[16], width
, height
;
640 if (vobsub_parse_ifo(NULL
,spudec_ifo
, palette
, &width
, &height
, 1, -1, NULL
) >= 0)
641 vo_spudec
=spudec_new_scaled(palette
, sh_video
->disp_w
, sh_video
->disp_h
);
644 if (vo_spudec
==NULL
) {
645 vo_spudec
=spudec_new_scaled(stream
->type
==STREAMTYPE_DVD
?((dvd_priv_t
*)(stream
->priv
))->cur_pgc
->palette
:NULL
,
646 sh_video
->disp_w
, sh_video
->disp_h
);
651 // Apply current settings for forced subs
652 spudec_set_forced_subs_only(vo_spudec
,forced_subs_only
);
654 // set up output file:
655 muxer_f
=fopen(out_filename
,"wb");
657 mp_msg(MSGT_MENCODER
, MSGL_FATAL
, MSGTR_CannotOpenOutputFile
, out_filename
);
658 mencoder_exit(1,NULL
);
661 muxer
=muxer_new_muxer(out_file_format
,muxer_f
);
663 // ============= VIDEO ===============
665 mux_v
=muxer_new_stream(muxer
,MUXER_TYPE_VIDEO
);
667 mux_v
->buffer_size
=0x200000; // 2MB
668 mux_v
->buffer
=malloc(mux_v
->buffer_size
);
670 mux_v
->source
=sh_video
;
672 mux_v
->h
.dwSampleSize
=0; // VBR
673 #ifdef USE_LIBAVCODEC
675 AVRational q
= av_d2q(force_ofps
?force_ofps
:sh_video
->fps
*playback_speed
, 30000);
676 mux_v
->h
.dwScale
= q
.den
;
677 mux_v
->h
.dwRate
= q
.num
;
680 mux_v
->h
.dwScale
=10000;
681 mux_v
->h
.dwRate
=mux_v
->h
.dwScale
*(force_ofps
?force_ofps
:sh_video
->fps
*playback_speed
);
684 mux_v
->codec
=out_video_codec
;
688 sh_video
->codec
=NULL
;
689 sh_video
->video_out
=NULL
;
690 sh_video
->vfilter
=NULL
; // fixme!
692 switch(mux_v
->codec
){
696 mux_v
->bih
=malloc(sh_video
->bih
->biSize
);
697 memcpy(mux_v
->bih
, sh_video
->bih
, sh_video
->bih
->biSize
);
701 mux_v
->bih
=calloc(1,sizeof(BITMAPINFOHEADER
));
702 mux_v
->bih
->biSize
=sizeof(BITMAPINFOHEADER
);
703 mux_v
->bih
->biWidth
=sh_video
->disp_w
;
704 mux_v
->bih
->biHeight
=sh_video
->disp_h
;
705 mux_v
->bih
->biCompression
=sh_video
->format
;
706 mux_v
->bih
->biPlanes
=1;
707 mux_v
->bih
->biBitCount
=24; // FIXME!!!
708 mux_v
->bih
->biSizeImage
=mux_v
->bih
->biWidth
*mux_v
->bih
->biHeight
*(mux_v
->bih
->biBitCount
/8);
711 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_VCodecFramecopy
,
712 mux_v
->bih
->biWidth
, mux_v
->bih
->biHeight
,
713 mux_v
->bih
->biBitCount
, mux_v
->bih
->biCompression
);
717 if ((mux_v
->bih
->biSize
!= sh_video
->bih
->biSize
) ||
718 memcmp(mux_v
->bih
, sh_video
->bih
, sh_video
->bih
->biSize
))
720 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_VCodecFramecopy
,
721 sh_video
->bih
->biWidth
, sh_video
->bih
->biHeight
,
722 sh_video
->bih
->biBitCount
, sh_video
->bih
->biCompression
);
723 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_FrameCopyFileMismatch
);
724 mencoder_exit(1,NULL
);
728 if ((mux_v
->bih
->biWidth
!= sh_video
->disp_w
) ||
729 (mux_v
->bih
->biHeight
!= sh_video
->disp_h
) ||
730 (mux_v
->bih
->biCompression
!= sh_video
->format
)) {
731 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_VCodecFramecopy
,
732 sh_video
->disp_w
, sh_video
->disp_w
, 24, sh_video
->format
);
733 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_FrameCopyFileMismatch
);
734 mencoder_exit(1,NULL
);
741 mux_v
->bih
=calloc(1,sizeof(BITMAPINFOHEADER
));
742 mux_v
->bih
->biSize
=sizeof(BITMAPINFOHEADER
);
743 mux_v
->bih
->biWidth
=sh_video
->disp_w
;
744 mux_v
->bih
->biHeight
=sh_video
->disp_h
;
745 mux_v
->bih
->biPlanes
=1;
746 mux_v
->bih
->biBitCount
=24;
747 mux_v
->bih
->biCompression
=mmioFOURCC('F','r','N','o');
748 mux_v
->bih
->biSizeImage
=mux_v
->bih
->biWidth
*mux_v
->bih
->biHeight
*(mux_v
->bih
->biBitCount
/8);
752 static vf_instance_t
* ve
= NULL
;
754 switch(mux_v
->codec
){
756 sh_video
->vfilter
=vf_open_encoder(NULL
,"divx4",(char *)mux_v
); break;
757 case VCODEC_LIBAVCODEC
:
758 sh_video
->vfilter
=vf_open_encoder(NULL
,"lavc",(char *)mux_v
); break;
760 sh_video
->vfilter
=vf_open_encoder(NULL
,"raw",(char *)mux_v
); break;
762 sh_video
->vfilter
=vf_open_encoder(NULL
,"vfw",(char *)mux_v
); break;
764 sh_video
->vfilter
=vf_open_encoder(NULL
,"libdv",(char *)mux_v
); break;
766 sh_video
->vfilter
=vf_open_encoder(NULL
,"xvid",(char *)mux_v
); break;
768 sh_video
->vfilter
=vf_open_encoder(NULL
,"qtvideo",(char *)mux_v
); break;
770 sh_video
->vfilter
=vf_open_encoder(NULL
,"nuv",(char *)mux_v
); break;
772 sh_video
->vfilter
=vf_open_encoder(NULL
,"x264",(char *)mux_v
); break;
774 if(!mux_v
->bih
|| !sh_video
->vfilter
){
775 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_EncoderOpenFailed
);
776 mencoder_exit(1,NULL
);
778 ve
= sh_video
->vfilter
;
779 } else sh_video
->vfilter
= ve
;
780 // append 'expand' filter, it fixes stride problems and renders osd:
782 char* vf_args
[] = { "osd", "1", NULL
};
783 sh_video
->vfilter
=vf_open_filter(sh_video
->vfilter
,"expand",vf_args
);
785 sh_video
->vfilter
=append_filters(sh_video
->vfilter
);
787 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
788 init_best_video_codec(sh_video
,video_codec_list
,video_fm_list
);
789 mp_msg(MSGT_CPLAYER
,MSGL_INFO
,"==========================================================================\n");
790 if(!sh_video
->inited
) mencoder_exit(1,NULL
);
795 /* force output fourcc to .. */
796 if ((force_fourcc
!= NULL
) && (strlen(force_fourcc
) >= 4))
798 mux_v
->bih
->biCompression
= mmioFOURCC(force_fourcc
[0], force_fourcc
[1],
799 force_fourcc
[2], force_fourcc
[3]);
800 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_ForcingOutputFourcc
,
801 mux_v
->bih
->biCompression
, (char *)&mux_v
->bih
->biCompression
);
804 if(muxer
->fix_stream_parameters
)
805 muxer_stream_fix_parameters(muxer
,mux_v
);
806 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
808 // ============= AUDIO ===============
811 mux_a
=muxer_new_stream(muxer
,MUXER_TYPE_AUDIO
);
813 mux_a
->buffer_size
=0x100000; //16384;
814 mux_a
->buffer
=malloc(mux_a
->buffer_size
);
816 mencoder_exit(1,MSGTR_MemAllocFailed
);
818 mux_a
->source
=sh_audio
;
820 mux_a
->codec
=out_audio_codec
;
822 ao_data
.samplerate
= force_srate
? force_srate
: new_srate
;
823 ao_data
.channels
= audio_output_channels
? audio_output_channels
: sh_audio
->channels
;
824 ao_data
.format
= audio_output_format
? audio_output_format
: sh_audio
->sample_format
;
825 if(!preinit_audio_filters(sh_audio
,
828 sh_audio
->channels
, sh_audio
->sample_format
,
830 &ao_data
.samplerate
, &ao_data
.channels
, &ao_data
.format
)) {
831 mp_msg(MSGT_CPLAYER
,MSGL_ERR
,MSGTR_AudioFilterChainPreinitError
);
832 mencoder_exit(1, NULL
);
835 aparams
.channels
= ao_data
.channels
;
836 aparams
.sample_rate
= force_srate
? force_srate
: ao_data
.samplerate
;
837 aparams
.audio_preload
= 1000 * audio_preload
;
838 if(mux_a
->codec
!= ACODEC_COPY
) {
839 aencoder
= new_audio_encoder(mux_a
, &aparams
);
841 mencoder_exit(1, NULL
);
842 if(!init_audio_filters(sh_audio
,
843 new_srate
, sh_audio
->channels
, sh_audio
->sample_format
,
844 aparams
.sample_rate
, aparams
.channels
, aencoder
->input_format
,
845 aencoder
->min_buffer_size
, aencoder
->max_buffer_size
)) {
846 mp_msg(MSGT_CPLAYER
,MSGL_FATAL
,MSGTR_NoMatchingFilter
);
847 mencoder_exit(1,NULL
);
850 switch(mux_a
->codec
){
852 if (playback_speed
!= 1.0) mp_msg(MSGT_CPLAYER
, MSGL_WARN
, MSGTR_NoSpeedWithFrameCopy
);
854 mux_a
->wf
=malloc(sizeof(WAVEFORMATEX
) + sh_audio
->wf
->cbSize
);
855 memcpy(mux_a
->wf
, sh_audio
->wf
, sizeof(WAVEFORMATEX
) + sh_audio
->wf
->cbSize
);
856 if(!sh_audio
->i_bps
) sh_audio
->i_bps
=mux_a
->wf
->nAvgBytesPerSec
;
858 mux_a
->wf
= malloc(sizeof(WAVEFORMATEX
));
859 mux_a
->wf
->nBlockAlign
= 1; //mux_a->h.dwSampleSize;
860 mux_a
->wf
->wFormatTag
= sh_audio
->format
;
861 mux_a
->wf
->nChannels
= sh_audio
->channels
;
862 mux_a
->wf
->nSamplesPerSec
= sh_audio
->samplerate
;
863 mux_a
->wf
->nAvgBytesPerSec
=sh_audio
->i_bps
; //mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
864 mux_a
->wf
->wBitsPerSample
= 16; // FIXME
865 mux_a
->wf
->cbSize
=0; // FIXME for l3codeca.acm
867 if(sh_audio
->audio
.dwScale
){
868 mux_a
->h
.dwSampleSize
=sh_audio
->audio
.dwSampleSize
;
869 mux_a
->h
.dwScale
=sh_audio
->audio
.dwScale
;
870 mux_a
->h
.dwRate
=sh_audio
->audio
.dwRate
;
871 // mux_a->h.dwStart=sh_audio->audio.dwStart;
873 mux_a
->h
.dwSampleSize
=mux_a
->wf
->nBlockAlign
;
874 mux_a
->h
.dwScale
=mux_a
->h
.dwSampleSize
;
875 mux_a
->h
.dwRate
=mux_a
->wf
->nAvgBytesPerSec
;
877 mux_a
->h
.dwRate
*= playback_speed
;
878 mux_a
->wf
->nSamplesPerSec
*= playback_speed
;
879 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_ACodecFramecopy
,
880 mux_a
->wf
->wFormatTag
, mux_a
->wf
->nChannels
, mux_a
->wf
->nSamplesPerSec
,
881 mux_a
->wf
->wBitsPerSample
, mux_a
->wf
->nAvgBytesPerSec
, mux_a
->h
.dwSampleSize
);
885 if (verbose
>1) print_wave_header(mux_a
->wf
);
887 if(audio_delay
!=0.0){
888 mux_a
->h
.dwStart
=audio_delay
*mux_a
->h
.dwRate
/mux_a
->h
.dwScale
;
889 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_SettingAudioDelay
,mux_a
->h
.dwStart
*mux_a
->h
.dwScale
/(float)mux_a
->h
.dwRate
);
891 if(muxer
->fix_stream_parameters
)
892 muxer_stream_fix_parameters(muxer
,mux_a
);
896 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_WritingAVIHeader
);
897 if (muxer
->cont_write_header
) muxer_write_header(muxer
);
901 signal(SIGINT
,exit_sighandler
); // Interrupt from keyboard
902 signal(SIGQUIT
,exit_sighandler
); // Quit from keyboard
903 signal(SIGTERM
,exit_sighandler
); // kill
905 timer_start
=GetTimerMS();
906 } // if (!curfile) // if this was the first file.
908 int out_format
= 0, out_minsize
= 0, out_maxsize
= 0;
909 int do_init_filters
= 1;
910 if((aencoder
!= NULL
) && (mux_a
->codec
!= ACODEC_COPY
))
912 out_format
= aencoder
->input_format
;
913 out_minsize
= aencoder
->min_buffer_size
;
914 out_maxsize
= aencoder
->max_buffer_size
;
916 switch(mux_a
->codec
){
919 if (playback_speed
!= 1.0) mp_msg(MSGT_CPLAYER
, MSGL_WARN
, MSGTR_NoSpeedWithFrameCopy
);
920 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_ACodecFramecopy
,
921 mux_a
->wf
->wFormatTag
, mux_a
->wf
->nChannels
, mux_a
->wf
->nSamplesPerSec
,
922 mux_a
->wf
->wBitsPerSample
, mux_a
->wf
->nAvgBytesPerSec
, mux_a
->h
.dwSampleSize
);
924 if ((mux_a
->wf
->wFormatTag
!= sh_audio
->wf
->wFormatTag
) ||
925 (mux_a
->wf
->nChannels
!= sh_audio
->wf
->nChannels
) ||
926 (mux_a
->wf
->nSamplesPerSec
!= sh_audio
->wf
->nSamplesPerSec
* playback_speed
))
928 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_ACodecFramecopy
,
929 sh_audio
->wf
->wFormatTag
, sh_audio
->wf
->nChannels
, sh_audio
->wf
->nSamplesPerSec
* playback_speed
,
930 sh_audio
->wf
->wBitsPerSample
, sh_audio
->wf
->nAvgBytesPerSec
, 0);
931 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_AudioCopyFileMismatch
);
932 mencoder_exit(1,NULL
);
936 if ((mux_a
->wf
->wFormatTag
!= sh_audio
->format
) ||
937 (mux_a
->wf
->nChannels
!= sh_audio
->channels
) ||
938 (mux_a
->wf
->nSamplesPerSec
!= sh_audio
->samplerate
* playback_speed
))
940 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_ACodecFramecopy
,
941 sh_audio
->wf
->wFormatTag
, sh_audio
->wf
->nChannels
, sh_audio
->wf
->nSamplesPerSec
* playback_speed
,
942 sh_audio
->wf
->wBitsPerSample
, sh_audio
->wf
->nAvgBytesPerSec
, 0);
943 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_AudioCopyFileMismatch
);
944 mencoder_exit(1,NULL
);
950 if (do_init_filters
) if(!init_audio_filters(sh_audio
,
953 sh_audio
->sample_format
,
954 mux_a
->wf
->nSamplesPerSec
,
955 mux_a
->wf
->nChannels
,
960 mp_msg(MSGT_CPLAYER
, MSGL_FATAL
, MSGTR_NoMatchingFilter
);
961 mencoder_exit(1, NULL
);
970 if (sscanf(seek_to_sec
, "%d:%d:%f", &a
,&b
,&d
)==3)
972 else if (sscanf(seek_to_sec
, "%d:%f", &a
, &d
)==2)
975 sscanf(seek_to_sec
, "%f", &d
);
977 demux_seek(demuxer
, d
, 1);
978 // there is 2 way to handle the -ss option in 3-pass mode:
979 // > 1. do the first pass for the whole file, and use -ss for 2nd/3rd pases only
980 // > 2. do all the 3 passes with the same -ss value
981 // this line enables behaviour 1. (and kills 2. at the same time):
982 // if(demuxer2) demux_seek(demuxer2, d, 1);
985 if (out_file_format
== MUXER_TYPE_MPEG
)
987 if (audio_preload
> 0.4) {
988 mp_msg(MSGT_MENCODER
, MSGL_WARN
, MSGTR_LimitingAudioPreload
);
991 if (audio_density
< 4) {
992 mp_msg(MSGT_MENCODER
, MSGL_WARN
, MSGTR_IncreasingAudioDensity
);
997 if(file_format
== DEMUXER_TYPE_TV
)
999 mp_msg(MSGT_MENCODER
, MSGL_WARN
, MSGTR_ZeroingAudioPreloadAndMaxPtsCorrection
);
1000 audio_preload
= 0.0;
1001 default_max_pts_correction
= 0;
1004 play_n_frames
=play_n_frames_mf
;
1005 if (curfile
&& end_at_type
== END_AT_TIME
) end_at
+= mux_v
->timer
;
1008 if (edl_records
) free_edl(edl_records
);
1009 next_edl_record
= edl_records
= NULL
;
1014 next_edl_record
= edl_records
= edl_parse_file();
1024 unsigned char* start
=NULL
;
1026 int skip_flag
=0; // 1=skip -1=duplicate
1028 if((end_at_type
== END_AT_SIZE
&& end_at
<= ftello(muxer_f
)) ||
1029 (end_at_type
== END_AT_TIME
&& end_at
< mux_v
->timer
))
1032 if(play_n_frames
>=0){
1034 if(play_n_frames
<0) break;
1039 if (next_edl_record
&& sh_video
&& sh_video
->pts
>= next_edl_record
->start_sec
) {
1040 if (next_edl_record
->action
== EDL_SKIP
&& edl_seeking
) {
1041 float last_pos
= d_video
->pts
;
1043 mp_msg(MSGT_CPLAYER
, MSGL_DBG4
, "EDL_SKIP: start [%f], stop [%f], length [%f]\n",
1044 next_edl_record
->start_sec
, next_edl_record
->stop_sec
, next_edl_record
->length_sec
);
1046 result
= edl_seek(next_edl_record
, demuxer
, d_audio
, mux_a
, &frame_time
, &start
, mux_v
->codec
==VCODEC_COPY
);
1048 if (result
== 2) break; // EOF
1049 else if (result
== 0) edl_seeking
= 0; // no seeking
1052 if (last_pos
>= sh_video
->pts
) {
1053 // backwards seek detected!! Forget about this EDL skip altogether.
1054 next_edl_record
= next_edl_record
->next
;
1056 else for (next_edl_record
= edl_records
; next_edl_record
; next_edl_record
= next_edl_record
->next
) {
1057 /* note the use of stop_sec,
1058 meaning if by some magical way we landed in the MIDDLE of a censored area,
1059 in the next loop it will jump out of it.
1061 if (next_edl_record
->stop_sec
> sh_video
->pts
) break; // we got to the right place.
1062 if (next_edl_record
->action
== EDL_MUTE
) edl_muted
= !edl_muted
; // toggle mute each time.
1065 /* for a pedantic EDL, that doesn't show even a single
1066 frame from the "censored" area, uncomment next line. */
1069 } else if (next_edl_record
->action
== EDL_MUTE
) {
1070 edl_muted
= !edl_muted
; // This variable does nothing for now.
1071 mp_msg(MSGT_CPLAYER
, MSGL_DBG4
, "EDL_MUTE: [%f]\n", next_edl_record
->start_sec
);
1072 next_edl_record
=next_edl_record
->next
;
1080 while(mux_a
->timer
-audio_preload
<mux_v
->timer
){
1084 ptimer_start
= GetTimerMS();
1085 // CBR - copy 0.5 sec of audio
1086 // or until the end of video:
1087 tottime
= stop_time(demuxer
, mux_v
);
1088 if (tottime
!= -1) {
1089 tottime
-= mux_a
->timer
;
1090 if (tottime
> 1./audio_density
) tottime
= 1./audio_density
;
1092 else tottime
= 1./audio_density
;
1097 if(mux_a
->h
.dwSampleSize
) /* CBR */
1099 if(aencoder
->set_decoded_len
)
1101 len
= mux_a
->h
.dwSampleSize
*(int)(mux_a
->h
.dwRate
*tottime
);
1102 aencoder
->set_decoded_len(aencoder
, len
);
1105 len
= aencoder
->decode_buffer_size
;
1107 len
= dec_audio(sh_audio
, aencoder
->decode_buffer
, len
);
1108 mux_a
->buffer_len
+= aencoder
->encode(aencoder
, mux_a
->buffer
+ mux_a
->buffer_len
,
1109 (void*)aencoder
->decode_buffer
, len
, mux_a
->buffer_size
-mux_a
->buffer_len
);
1110 if(mux_a
->buffer_len
< mux_a
->wf
->nBlockAlign
)
1113 len
= mux_a
->wf
->nBlockAlign
*(mux_a
->buffer_len
/mux_a
->wf
->nBlockAlign
);
1122 sz
= aencoder
->get_frame_size(aencoder
);
1123 if(sz
> 0 && mux_a
->buffer_len
>= sz
)
1128 len
= dec_audio(sh_audio
,aencoder
->decode_buffer
, aencoder
->decode_buffer_size
);
1134 len
= aencoder
->encode(aencoder
, mux_a
->buffer
+ mux_a
->buffer_len
, (void*)aencoder
->decode_buffer
, len
, mux_a
->buffer_size
-mux_a
->buffer_len
);
1135 mux_a
->buffer_len
+= len
;
1138 if (mux_v
->timer
== 0) mux_a
->h
.dwInitialFrames
++;
1141 if(mux_a
->h
.dwSampleSize
){
1142 switch(mux_a
->codec
){
1143 case ACODEC_COPY
: // copy
1144 len
=mux_a
->wf
->nAvgBytesPerSec
*tottime
;
1145 len
/=mux_a
->h
.dwSampleSize
;if(len
<1) len
=1;
1146 len
*=mux_a
->h
.dwSampleSize
;
1147 len
=demux_read_data(sh_audio
->ds
,mux_a
->buffer
,len
);
1151 // VBR - encode/copy an audio frame
1152 switch(mux_a
->codec
){
1153 case ACODEC_COPY
: // copy
1154 len
=ds_get_packet(sh_audio
->ds
,(unsigned char**) &mux_a
->buffer
);
1159 if(len
<=0) break; // EOF?
1160 muxer_write_chunk(mux_a
,len
,0x10);
1161 if(!mux_a
->h
.dwSampleSize
&& mux_a
->timer
>0)
1162 mux_a
->wf
->nAvgBytesPerSec
=0.5f
+(double)mux_a
->size
/mux_a
->timer
; // avg bps (VBR)
1163 if(mux_a
->buffer_len
>=len
){
1164 mux_a
->buffer_len
-=len
;
1165 memcpy(mux_a
->buffer
,mux_a
->buffer
+len
,mux_a
->buffer_len
);
1170 audiorate
+= (GetTimerMS() - ptimer_start
);
1172 // let's not output more audio than necessary
1173 if (stop_time(demuxer
, mux_v
) != -1 && stop_time(demuxer
, mux_v
) <= mux_a
->timer
) break;
1180 if (edl_skip
!= -1) { in_size
= edl_skip
; edl_skip
= -1; }
1183 in_size
=video_read_frame(sh_video
,&frame_time
,&start
,force_fps
);
1184 if(in_size
<0){ at_eof
=1; break; }
1185 sh_video
->timer
+=frame_time
; ++decoded_frameno
;
1186 frame_time
/= playback_speed
;
1188 v_timer_corr
-=frame_time
-(float)mux_v
->h
.dwScale
/mux_v
->h
.dwRate
;
1190 if(demuxer2
){ // 3-pass encoding, read control file (frameno.avi)
1192 while(next_frameno
<decoded_frameno
){
1194 int len
=ds_get_packet(demuxer2
->video
,(unsigned char**) &start
);
1195 if(len
<0){ at_eof
=1;break;}
1196 if(len
==0) --skip_flag
; else // duplicate
1197 if(len
==4) next_frameno
=start
[0];
1200 // if(skip_flag) printf("!!!!!!!!!!!!\n");
1201 skip_flag
=next_frameno
-decoded_frameno
;
1203 while(next_frameno
<=decoded_frameno
){
1205 int len
=ds_get_packet(demuxer2
->video
,(unsigned char**) &start
);
1206 if(len
<0){ at_eof
=1;break;}
1207 if(len
==0) --skip_flag
; else // duplicate
1208 if(len
==4) next_frameno
=start
[0];
1210 // if(at_eof) break;
1211 // printf("Current fno=%d requested=%d skip=%d \n",decoded_frameno,fno,skip_flag);
1214 // check frame duplicate/drop:
1216 //printf("\r### %5.3f ###\n",v_timer_corr);
1218 if(v_timer_corr
>=(float)mux_v
->h
.dwScale
/mux_v
->h
.dwRate
&&
1219 (skip_limit
<0 || skip_flag
<skip_limit
) ){
1220 v_timer_corr
-=(float)mux_v
->h
.dwScale
/mux_v
->h
.dwRate
;
1221 ++skip_flag
; // skip
1223 while(v_timer_corr
<=-(float)mux_v
->h
.dwScale
/mux_v
->h
.dwRate
&&
1224 (skip_limit
<0 || (-skip_flag
)<skip_limit
) ){
1225 v_timer_corr
+=(float)mux_v
->h
.dwScale
/mux_v
->h
.dwRate
;
1229 while( (v_pts_corr
<=-(float)mux_v
->h
.dwScale
/mux_v
->h
.dwRate
&& skip_flag
>0)
1230 || (v_pts_corr
<=-2*(float)mux_v
->h
.dwScale
/mux_v
->h
.dwRate
) ){
1231 v_pts_corr
+=(float)mux_v
->h
.dwScale
/mux_v
->h
.dwRate
;
1234 if( (v_pts_corr
>=(float)mux_v
->h
.dwScale
/mux_v
->h
.dwRate
&& skip_flag
<0)
1235 || (v_pts_corr
>=2*(float)mux_v
->h
.dwScale
/mux_v
->h
.dwRate
) )
1236 if(skip_flag
<=0){ // we can't skip more than 1 frame now
1237 v_pts_corr
-=(float)mux_v
->h
.dwScale
/mux_v
->h
.dwRate
;
1238 ++skip_flag
; // skip
1243 ptimer_start
= GetTimerMS();
1245 switch(mux_v
->codec
){
1247 mux_v
->buffer
=start
;
1248 if(skip_flag
<=0) muxer_write_chunk(mux_v
,in_size
,(sh_video
->ds
->flags
&1)?0x10:0);
1250 case VCODEC_FRAMENO
:
1251 mux_v
->buffer
=(unsigned char *)&decoded_frameno
; // tricky
1252 if(skip_flag
<=0) muxer_write_chunk(mux_v
,sizeof(int),0x10);
1255 // decode_video will callback down to ve_*.c encoders, through the video filters
1256 blit_frame
=decode_video(sh_video
,start
,in_size
,
1257 skip_flag
>0 && (!sh_video
->vfilter
|| ((vf_instance_t
*)sh_video
->vfilter
)->control(sh_video
->vfilter
, VFCTRL_SKIP_NEXT_FRAME
, 0) != CONTROL_TRUE
));
1259 if (sh_video
->vf_inited
< 0) mencoder_exit(1, NULL
);
1264 // unwanted skipping of a frame, what to do?
1266 // skipping not allowed -> write empty frame:
1267 if (!encode_duplicates
|| !sh_video
->vfilter
|| ((vf_instance_t
*)sh_video
->vfilter
)->control(sh_video
->vfilter
, VFCTRL_DUPLICATE_FRAME
, 0) != CONTROL_TRUE
)
1268 muxer_write_chunk(mux_v
,0,0);
1270 // skipping allowed -> skip it and distriubute timer error:
1271 v_timer_corr
-=(float)mux_v
->h
.dwScale
/mux_v
->h
.dwRate
;
1278 videorate
+=(GetTimerMS() - ptimer_start
);
1282 if(!quiet
) mp_msg(MSGT_MENCODER
, MSGL_WARN
, MSGTR_DuplicateFrames
,-skip_flag
);
1285 if (!encode_duplicates
|| !sh_video
->vfilter
|| ((vf_instance_t
*)sh_video
->vfilter
)->control(sh_video
->vfilter
, VFCTRL_DUPLICATE_FRAME
, 0) != CONTROL_TRUE
)
1286 muxer_write_chunk(mux_v
,0,0);
1292 if(!quiet
) mp_msg(MSGT_MENCODER
, MSGL_WARN
, MSGTR_SkipFrame
);
1297 if(sh_audio
&& !demuxer2
){
1302 unsigned int samples
=(sh_audio
->audio
.dwSampleSize
)?
1303 ((ds_tell(d_audio
)-sh_audio
->a_in_buffer_len
)/sh_audio
->audio
.dwSampleSize
) :
1304 (d_audio
->block_no
); // <- used for VBR audio
1305 // printf("samples=%d \n",samples);
1306 a_pts
=samples
*(float)sh_audio
->audio
.dwScale
/(float)sh_audio
->audio
.dwRate
;
1311 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
1313 if(!delay_corrected
) if(a_pts
) delay_corrected
=1;
1314 //printf("*** %5.3f ***\n",a_pts);
1315 a_pts
+=(ds_tell_pts(d_audio
)-sh_audio
->a_in_buffer_len
)/(float)sh_audio
->i_bps
;
1317 v_pts
=sh_video
? sh_video
->pts
: d_video
->pts
;
1318 // av = compensated (with out buffering delay) A-V diff
1319 AV_delay
=(a_pts
-v_pts
);
1320 AV_delay
/= playback_speed
;
1321 AV_delay
-=mux_a
->timer
-(mux_v
->timer
-(v_timer_corr
+v_pts_corr
));
1322 // compensate input video timer by av:
1324 if(x
<-max_pts_correction
) x
=-max_pts_correction
; else
1325 if(x
> max_pts_correction
) x
= max_pts_correction
;
1326 if(default_max_pts_correction
>=0)
1327 max_pts_correction
=default_max_pts_correction
*playback_speed
;
1329 max_pts_correction
=sh_video
->frametime
*0.10 *playback_speed
; // +-10% of time
1330 // sh_video->timer-=x;
1335 // printf("A:%6.1f V:%6.1f A-V:%7.3f oAV:%7.3f diff:%7.3f ct:%7.3f vpc:%7.3f \r",
1336 // a_pts,v_pts,a_pts-v_pts,
1337 // (float)(mux_a->timer-mux_v->timer),
1338 // AV_delay, c_total, v_pts_corr );
1339 // printf("V:%6.1f \r", d_video->pts );
1342 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",
1343 a_pts
,v_pts
,a_pts
-v_pts
,c_total
,
1344 (int)sh_video
->num_frames
,(int)sh_video
->num_frames_decoded
,
1345 (sh_video
->timer
>0.5)?(int)(100.0*video_time_usage
/(double)sh_video
->timer
):0,
1346 (sh_video
->timer
>0.5)?(int)(100.0*vout_time_usage
/(double)sh_video
->timer
):0,
1347 (sh_video
->timer
>0.5)?(100.0*audio_time_usage
/(double)sh_video
->timer
):0
1352 { float t
=(GetTimerMS()-timer_start
)*0.001f
;
1353 float len
=(demuxer
->movi_end
-demuxer
->movi_start
);
1354 float p
=len
>1000 ? (float)(demuxer
->filepos
-demuxer
->movi_start
) / len
: 0;
1356 if(!len
&& sh_audio
&& sh_audio
->audio
.dwLength
>100){
1357 p
=(sh_audio
->audio
.dwSampleSize
? ds_tell(sh_audio
->ds
)/sh_audio
->audio
.dwSampleSize
: sh_audio
->ds
->block_no
)
1358 / (float)(sh_audio
->audio
.dwLength
);
1362 mp_msg(MSGT_AVSYNC
,MSGL_STATUS
,"%d < %d < %d \r",
1363 (int)demuxer
->movi_start
,
1364 (int)demuxer
->filepos
,
1365 (int)demuxer
->movi_end
);
1369 mp_msg(MSGT_AVSYNC
,MSGL_STATUS
,"Pos:%6.1fs %6df (%2d%%) %3dfps Trem:%4dmin %3dmb A-V:%5.3f [%d:%d] A/Vms %d/%d D/B/S %d/%d/%d \r",
1370 mux_v
->timer
, decoded_frameno
, (int)(p
*100),
1371 (t
>1) ? (int)(decoded_frameno
/t
+0.5) : 0,
1372 (p
>0.001) ? (int)((t
/p
-t
)/60) : 0,
1373 (p
>0.001) ? (int)(ftello(muxer_f
)/p
/1024/1024) : 0,
1375 (mux_v
->timer
>1) ? (int)(mux_v
->size
/mux_v
->timer
/125) : 0,
1376 (mux_a
&& mux_a
->timer
>1) ? (int)(mux_a
->size
/mux_a
->timer
/125) : 0,
1377 audiorate
/audiosamples
, videorate
/videosamples
,
1378 duplicatedframes
, badframes
, skippedframes
1381 mp_msg(MSGT_AVSYNC
,MSGL_STATUS
,"Pos:%6.1fs %6df (%2d%%) %3dfps Trem:%4dmin %3dmb A-V:%5.3f [%d:%d]\r",
1382 mux_v
->timer
, decoded_frameno
, (int)(p
*100),
1383 (t
>1) ? (int)(decoded_frameno
/t
+0.5) : 0,
1384 (p
>0.001) ? (int)((t
/p
-t
)/60) : 0,
1385 (p
>0.001) ? (int)(ftello(muxer_f
)/p
/1024/1024) : 0,
1387 (mux_v
->timer
>1) ? (int)(mux_v
->size
/mux_v
->timer
/125) : 0,
1388 (mux_a
&& mux_a
->timer
>1) ? (int)(mux_a
->size
/mux_a
->timer
/125) : 0
1397 if(subdata
&& sh_video
->pts
>0){
1398 float pts
=sh_video
->pts
;
1399 if(sub_fps
==0) sub_fps
=sh_video
->fps
;
1400 if (pts
> sub_last_pts
|| pts
< sub_last_pts
-1.0 ) {
1401 find_sub(subdata
, (pts
+sub_delay
) *
1402 (subdata
->sub_uses_time
? 100. : sub_fps
));
1403 // FIXME! frame counter...
1411 if(vo_spudec
||vobsub_writer
){
1412 unsigned char* packet
=NULL
;
1414 while((len
=ds_get_packet_sub(d_dvdsub
,&packet
))>0){
1415 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
);
1417 spudec_assemble(vo_spudec
,packet
,len
,90000*d_dvdsub
->pts
);
1419 vobsub_out_output(vobsub_writer
,packet
,len
,mux_v
->timer
+ d_dvdsub
->pts
- sh_video
->pts
);
1422 spudec_heartbeat(vo_spudec
,90000*sh_video
->pts
);
1423 vo_osd_changed(OSDTYPE_SPU
);
1428 if(ferror(muxer_f
)) {
1429 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_ErrorWritingFile
, out_filename
);
1430 mencoder_exit(1, NULL
);
1435 if (!interrupted
&& filelist
[++curfile
].name
!= 0) {
1436 if (sh_video
&& sh_video
->vfilter
) {
1437 // Before uniniting sh_video and the filter chain, break apart the VE.
1438 vf_instance_t
* ve
; // this will be the filter right before the ve.
1439 for (ve
= sh_video
->vfilter
; ve
->next
&& ve
->next
->next
; ve
= ve
->next
);
1441 ve
->next
= NULL
; // I'm telling the last filter, before the VE, there is nothing after it
1442 else // There is no chain except the VE.
1443 sh_video
->vfilter
= NULL
;
1446 if(sh_video
){ uninit_video(sh_video
);sh_video
=NULL
; }
1447 if(demuxer
) free_demuxer(demuxer
);
1448 if(stream
) free_stream(stream
); // kill cache thread
1452 m_config_pop(mconfig
);
1453 goto play_next_file
;
1456 /* Emit the remaining frames in the video system */
1457 /*TODO emit frmaes delayed by decoder lag*/
1458 if(sh_video
&& sh_video
->vfilter
){
1459 mp_msg(MSGT_MENCODER
, MSGL_INFO
, "\nFlushing video frames\n");
1460 ((vf_instance_t
*)sh_video
->vfilter
)->control(sh_video
->vfilter
,
1461 VFCTRL_FLUSH_FRAMES
, 0);
1466 aencoder
->fixup(aencoder
);
1468 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_WritingAVIIndex
);
1469 if (muxer
->cont_write_index
) muxer_write_index(muxer
);
1470 muxer_f_size
=ftello(muxer_f
);
1471 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_FixupAVIHeader
);
1472 fseek(muxer_f
,0,SEEK_SET
);
1473 if (muxer
->cont_write_header
) muxer_write_header(muxer
); // update header
1474 if(ferror(muxer_f
) || fclose(muxer_f
) != 0) {
1475 mp_msg(MSGT_MENCODER
,MSGL_FATAL
,MSGTR_ErrorWritingFile
, out_filename
);
1476 mencoder_exit(1, NULL
);
1479 vobsub_out_close(vobsub_writer
);
1481 if(out_video_codec
==VCODEC_FRAMENO
&& mux_v
->timer
>100){
1482 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_RecommendedVideoBitrate
,"650MB",(int)((650*1024*1024-muxer_f_size
)/mux_v
->timer
/125));
1483 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_RecommendedVideoBitrate
,"700MB",(int)((700*1024*1024-muxer_f_size
)/mux_v
->timer
/125));
1484 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_RecommendedVideoBitrate
,"800MB",(int)((800*1024*1024-muxer_f_size
)/mux_v
->timer
/125));
1485 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_RecommendedVideoBitrate
,"2 x 650MB",(int)((2*650*1024*1024-muxer_f_size
)/mux_v
->timer
/125));
1486 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_RecommendedVideoBitrate
,"2 x 700MB",(int)((2*700*1024*1024-muxer_f_size
)/mux_v
->timer
/125));
1487 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_RecommendedVideoBitrate
,"2 x 800MB",(int)((2*800*1024*1024-muxer_f_size
)/mux_v
->timer
/125));
1490 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_VideoStreamResult
,
1491 (float)(mux_v
->size
/mux_v
->timer
*8.0f
/1000.0f
), (int)(mux_v
->size
/mux_v
->timer
), (int)mux_v
->size
, (float)mux_v
->timer
, decoded_frameno
);
1493 mp_msg(MSGT_MENCODER
, MSGL_INFO
, MSGTR_AudioStreamResult
,
1494 (float)(mux_a
->size
/mux_a
->timer
*8.0f
/1000.0f
), (int)(mux_a
->size
/mux_a
->timer
), (int)mux_a
->size
, (float)mux_a
->timer
);
1496 if(sh_video
){ uninit_video(sh_video
);sh_video
=NULL
; }
1497 if(demuxer
) free_demuxer(demuxer
);
1498 if(stream
) free_stream(stream
); // kill cache thread
1503 static void parse_end_at()
1506 end_at_type
= END_AT_NONE
;
1507 if (!end_at_string
) return;
1509 /* End at size parsing */
1513 end_at_type
= END_AT_SIZE
;
1515 if(sscanf(end_at_string
, "%lf%3s", &end_at
, unit
) == 2) {
1516 if(!strcasecmp(unit
, "b"))
1518 else if(!strcasecmp(unit
, "kb"))
1520 else if(!strcasecmp(unit
, "mb"))
1521 end_at
*= 1024*1024;
1523 end_at_type
= END_AT_NONE
;
1526 end_at_type
= END_AT_NONE
;
1529 /* End at time parsing. This has to be last because of
1530 * sscanf("%f", ...) below */
1531 if(end_at_type
== END_AT_NONE
)
1535 end_at_type
= END_AT_TIME
;
1537 if (sscanf(end_at_string
, "%d:%d:%f", &a
, &b
, &d
) == 3)
1538 end_at
= 3600*a
+ 60*b
+ d
;
1539 else if (sscanf(end_at_string
, "%d:%f", &a
, &d
) == 2)
1541 else if (sscanf(end_at_string
, "%f", &d
) == 1)
1544 end_at_type
= END_AT_NONE
;
1549 /* Flip the image in src and store the result in dst. src and dst may overlap.
1550 width is the size of each line in bytes. */
1551 static uint8_t* flip_upside_down(uint8_t* dst
, const uint8_t* src
, int width
,
1554 uint8_t* tmp
= malloc(width
);
1557 for(i
= 0; i
< height
/2; i
++) {
1558 memcpy(tmp
, &src
[i
*width
], width
);
1559 memcpy(&dst
[i
* width
], &src
[(height
- i
) * width
], width
);
1560 memcpy(&dst
[(height
- i
) * width
], tmp
, width
);
1569 static float stop_time(demuxer_t
* demuxer
, muxer_stream_t
* mux_v
) {
1570 float timeleft
= -1;
1571 if (play_n_frames
>= 0) timeleft
= mux_v
->timer
+ play_n_frames
* (double)(mux_v
->h
.dwScale
) / mux_v
->h
.dwRate
;
1572 if (end_at_type
== END_AT_TIME
&& (timeleft
> end_at
|| timeleft
== -1)) timeleft
= end_at
;
1574 if (next_edl_record
&& demuxer
&& demuxer
->video
) { // everything is OK to be checked
1575 float tmp
= mux_v
->timer
+ next_edl_record
->start_sec
- demuxer
->video
->pts
;
1576 if (timeleft
== -1 || timeleft
> tmp
) {
1577 // There's less time in EDL than what we already know
1578 if (next_edl_record
->action
== EDL_SKIP
&& edl_seeking
) {
1580 } else if (next_edl_record
->action
== EDL_MUTE
) {
1581 //timeleft = next_edl_record->start_sec - demuxer->video->pts;
1582 // For the moment (and probably forever) EDL mute doesn't work in MEncoder
1591 int edl_seek(edl_record_ptr next_edl_record
, demuxer_t
* demuxer
, demux_stream_t
*d_audio
, muxer_stream_t
* mux_a
, float * frame_time
, unsigned char ** start
, int framecopy
) {
1592 sh_audio_t
* sh_audio
= d_audio
->sh
;
1593 sh_video_t
* sh_video
= demuxer
->video
? demuxer
->video
->sh
: NULL
;
1594 vf_instance_t
* vfilter
= sh_video
? sh_video
->vfilter
: NULL
;
1596 if (!sh_video
) return 0;
1597 if (sh_video
->pts
>= next_edl_record
->stop_sec
) return 1; // nothing to do...
1599 if (!edl_seek_type
) {
1600 if(demux_seek(demuxer
, next_edl_record
->stop_sec
- sh_video
->pts
, 0)){
1601 sh_video
->pts
= demuxer
->video
->pts
;
1602 //if (vo_vobsub) vobsub_seek(vo_vobsub,sh_video->pts);
1603 resync_video_stream(sh_video
);
1604 //if(vo_spudec) spudec_reset(vo_spudec);
1607 // non-seekable stream.
1611 // slow seek, read every frame.
1613 while (!interrupted
) {
1617 in_size
= video_read_frame(sh_video
, frame_time
, start
, force_fps
);
1618 if(in_size
<0) return 2;
1619 sh_video
->timer
+= *frame_time
;
1622 a_pts
= d_audio
->pts
+ (ds_tell_pts(d_audio
) - sh_audio
->a_in_buffer_len
)/(float)sh_audio
->i_bps
;
1623 while (sh_video
->pts
> a_pts
) {
1625 if (mux_a
->h
.dwSampleSize
) {
1626 len
= mux_a
->wf
->nAvgBytesPerSec
* (sh_video
->pts
- a_pts
);
1627 len
/= mux_a
->h
.dwSampleSize
; if(len
<1) len
=1;
1628 len
*= mux_a
->h
.dwSampleSize
;
1629 len
= demux_read_data(sh_audio
->ds
,mux_a
->buffer
,len
);
1631 len
= ds_get_packet(sh_audio
->ds
,(unsigned char**) &mux_a
->buffer
);
1633 if (len
<= 0) break; // EOF of audio.
1634 a_pts
= d_audio
->pts
+ (ds_tell_pts(d_audio
)-sh_audio
->a_in_buffer_len
)/(float)sh_audio
->i_bps
;
1638 if (sh_video
->pts
>= next_edl_record
->stop_sec
) {
1640 if (!framecopy
|| (sh_video
->ds
->flags
& 1)) return 1;
1644 int softskip
= (vfilter
->control(vfilter
, VFCTRL_SKIP_NEXT_FRAME
, 0) == CONTROL_TRUE
);
1645 decode_video(sh_video
, *start
, in_size
, !softskip
);
1648 mp_msg(MSGT_MENCODER
, MSGL_STATUS
,
1649 "EDL SKIP: Start: %.2f End: %.2lf Current: V: %.2f A: %.2f \r",
1650 next_edl_record
->start_sec
, next_edl_record
->stop_sec
,
1651 sh_video
->pts
, a_pts
);
1653 if (interrupted
) return 2;