Forced subs support for mencoder
[mplayer/glamo.git] / mencoder.c
blob5b5035aab3df3c2d2772ea8bb1bd40e64687d3d4
1 #define VCODEC_COPY 0
2 #define VCODEC_FRAMENO 1
3 // real codecs:
4 #define VCODEC_DIVX4 2
5 #define VCODEC_LIBAVCODEC 4
6 #define VCODEC_RAWRGB 6
7 #define VCODEC_VFW 7
8 #define VCODEC_LIBDV 8
9 #define VCODEC_XVID 9
10 #define VCODEC_QTVIDEO 10
11 #define VCODEC_NUV 11
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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <signal.h>
23 #ifdef __MINGW32__
24 #define SIGQUIT 3
25 #endif
26 #include <sys/time.h>
28 #include "config.h"
30 #include "version.h"
31 #include "mp_msg.h"
32 #include "help_mp.h"
34 #include "cpudetect.h"
36 #include "codec-cfg.h"
37 #include "m_option.h"
38 #include "m_config.h"
39 #include "parser-mecmd.h"
41 #include "libmpdemux/stream.h"
42 #include "libmpdemux/demuxer.h"
43 #include "libmpdemux/stheader.h"
44 #include "libmpdemux/mp3_hdr.h"
45 #include "libmpdemux/muxer.h"
48 #include "libvo/video_out.h"
50 #include "libao2/afmt.h"
52 #include "libmpcodecs/mp_image.h"
53 #include "libmpcodecs/dec_audio.h"
54 #include "libmpcodecs/dec_video.h"
55 #include "libmpcodecs/vf.h"
57 // for MPEGLAYER3WAVEFORMAT:
58 #include "loader/wine/mmreg.h"
60 #ifdef HAVE_MP3LAME
61 #undef CDECL
62 #include <lame/lame.h>
63 #endif
65 #ifdef HAVE_LIBCSS
66 #include "libmpdemux/dvdauth.h"
67 #endif
69 #include <inttypes.h>
71 #include "libvo/fastmemcpy.h"
73 #include "osdep/timer.h"
75 #ifdef USE_LIBAVCODEC
76 // for lavc audio encoding
78 #ifdef USE_LIBAVCODEC_SO
79 #include <ffmpeg/avcodec.h>
80 #else
81 #include "libavcodec/avcodec.h"
82 #endif
84 static AVCodec *lavc_acodec;
85 static AVCodecContext *lavc_actx = NULL;
86 extern char *lavc_param_acodec;
87 extern int lavc_param_abitrate;
88 extern int lavc_param_atag;
89 // tmp buffer for lavc audio encoding (to free!!!!!)
90 static void *lavc_abuf = NULL;
91 extern int avcodec_inited;
93 static uint32_t lavc_find_atag(char *codec);
94 #endif
96 int vo_doublebuffering=0;
97 int vo_directrendering=0;
98 int vo_config_count=0;
99 int forced_subs_only=0;
101 //--------------------------
103 // cache2:
104 int stream_cache_size=-1;
105 #ifdef USE_STREAM_CACHE
106 extern int cache_fill_status;
107 #else
108 #define cache_fill_status 0
109 #endif
111 int audio_id=-1;
112 int video_id=-1;
113 int dvdsub_id=-1;
114 int vobsub_id=-1;
115 char* audio_lang=NULL;
116 char* dvdsub_lang=NULL;
117 static char* spudec_ifo=NULL;
119 static char** audio_codec_list=NULL; // override audio codec
120 static char** video_codec_list=NULL; // override video codec
121 static char** audio_fm_list=NULL; // override audio codec family
122 static char** video_fm_list=NULL; // override video codec family
124 static int out_audio_codec=-1;
125 static int out_video_codec=-1;
127 int out_file_format=MUXER_TYPE_AVI; // default to AVI
129 // audio stream skip/resync functions requires only for seeking.
130 // (they should be implemented in the audio codec layer)
131 //void skip_audio_frame(sh_audio_t *sh_audio){}
132 //void resync_audio_stream(sh_audio_t *sh_audio){}
134 int verbose=0; // must be global!
135 double video_time_usage=0;
136 double vout_time_usage=0;
137 double max_video_time_usage=0;
138 double max_vout_time_usage=0;
139 double cur_video_time_usage=0;
140 double cur_vout_time_usage=0;
141 int benchmark=0;
143 // A-V sync:
144 int delay_corrected=1;
145 static float default_max_pts_correction=-1;//0.01f;
146 static float max_pts_correction=0;//default_max_pts_correction;
147 static float c_total=0;
149 static float audio_preload=0.5;
150 static float audio_delay=0.0;
151 static int audio_density=2;
153 float force_fps=0;
154 static float force_ofps=0; // set to 24 for inverse telecine
155 static int skip_limit=-1;
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"
176 // sub:
177 char *font_name=NULL;
178 float font_factor=0.75;
179 char **sub_name=NULL;
180 float sub_delay=0;
181 float sub_fps=0;
182 int sub_auto = 0;
183 int subcc_enabled=0;
184 int suboverlap_enabled = 1;
186 #ifdef USE_SUB
187 static sub_data* subdata=NULL;
188 float sub_last_pts = -303;
189 #endif
191 int auto_expand=1;
193 // infos are empty by default
194 char *info_name=NULL;
195 char *info_artist=NULL;
196 char *info_genre=NULL;
197 char *info_subject=NULL;
198 char *info_copyright=NULL;
199 char *info_sourceform=NULL;
200 char *info_comment=NULL;
204 //char *out_audio_codec=NULL; // override audio codec
205 //char *out_video_codec=NULL; // override video codec
207 //#include "libmpeg2/mpeg2.h"
208 //#include "libmpeg2/mpeg2_internal.h"
210 #ifdef HAVE_MP3LAME
211 int lame_param_quality=0; // best
212 int lame_param_algqual=5; // same as old default
213 int lame_param_vbr=vbr_default;
214 int lame_param_mode=-1; // unset
215 int lame_param_padding=-1; // unset
216 int lame_param_br=-1; // unset
217 int lame_param_ratio=-1; // unset
218 float lame_param_scale=-1; // unset
219 #if HAVE_MP3LAME >= 392
220 int lame_param_fast=0; // unset
221 static char* lame_param_preset=NULL; // unset
222 static int lame_presets_set( lame_t gfp, int fast, int cbr, const char* preset_name );
223 static void lame_presets_longinfo_dm ( FILE* msgfp );
224 #endif
225 #endif
227 //static int vo_w=0, vo_h=0;
229 //-------------------------- config stuff:
231 m_config_t* mconfig;
233 extern int m_config_parse_config_file(m_config_t* config, char *conffile);
235 static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
237 static int cfg_include(m_option_t *conf, char *filename){
238 return m_config_parse_config_file(mconfig, filename);
241 static char *seek_to_sec=NULL;
242 static off_t seek_to_byte=0;
244 static int parse_end_at(m_option_t *conf, const char* param);
245 //static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width, int height);
247 #include "get_path.c"
249 #include "cfg-mplayer-def.h"
250 #include "cfg-mencoder.h"
252 #ifdef USE_DVDREAD
253 #include "spudec.h"
254 #endif
255 #include "vobsub.h"
257 /* FIXME */
258 static void mencoder_exit(int level, char *how)
260 if (how)
261 printf("Exiting... (%s)\n", how);
262 else
263 printf("Exiting...\n");
265 exit(level);
268 void parse_cfgfiles( m_config_t* conf )
270 char *conffile;
271 if ((conffile = get_path("mencoder")) == NULL) {
272 mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
273 } else {
274 if (m_config_parse_config_file(conf, conffile) < 0)
275 mencoder_exit(1,"configfile error");
276 free(conffile);
281 //---------------------------------------------------------------------------
283 static int dec_audio(sh_audio_t *sh_audio,unsigned char* buffer,int total){
284 int size=0;
285 int at_eof=0;
286 while(size<total && !at_eof){
287 int len=total-size;
288 if(len>MAX_OUTBURST) len=MAX_OUTBURST;
289 if(len>sh_audio->a_out_buffer_size) len=sh_audio->a_out_buffer_size;
290 if(len>sh_audio->a_out_buffer_len){
291 int ret=decode_audio(sh_audio,
292 &sh_audio->a_out_buffer[sh_audio->a_out_buffer_len],
293 len-sh_audio->a_out_buffer_len,
294 sh_audio->a_out_buffer_size-sh_audio->a_out_buffer_len);
295 if(ret>0) sh_audio->a_out_buffer_len+=ret; else at_eof=1;
297 if(len>sh_audio->a_out_buffer_len) len=sh_audio->a_out_buffer_len;
298 memcpy(buffer+size,sh_audio->a_out_buffer,len);
299 sh_audio->a_out_buffer_len-=len; size+=len;
300 if(sh_audio->a_out_buffer_len>0)
301 memcpy(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[len],sh_audio->a_out_buffer_len);
303 return size;
306 //---------------------------------------------------------------------------
308 static int at_eof=0;
309 static int interrupted=0;
311 enum end_at_type_t {END_AT_NONE, END_AT_TIME, END_AT_SIZE};
312 static enum end_at_type_t end_at_type = END_AT_NONE;
313 static double end_at;
315 static void exit_sighandler(int x){
316 at_eof=1;
317 interrupted=1;
320 static muxer_t* muxer=NULL;
321 static FILE* muxer_f=NULL;
323 extern void print_wave_header(WAVEFORMATEX *h);
325 int main(int argc,char* argv[]){
327 stream_t* stream=NULL;
328 demuxer_t* demuxer=NULL;
329 stream_t* stream2=NULL;
330 demuxer_t* demuxer2=NULL;
331 demux_stream_t *d_audio=NULL;
332 demux_stream_t *d_video=NULL;
333 demux_stream_t *d_dvdsub=NULL;
334 sh_audio_t *sh_audio=NULL;
335 sh_video_t *sh_video=NULL;
336 int file_format=DEMUXER_TYPE_UNKNOWN;
337 int i;
338 void *vobsub_writer=NULL;
340 uint32_t ptimer_start;
341 uint32_t audiorate=0;
342 uint32_t videorate=0;
343 uint32_t audiosamples=1;
344 uint32_t videosamples=1;
345 uint32_t skippedframes=0;
346 uint32_t duplicatedframes=0;
347 uint32_t badframes=0;
349 muxer_stream_t* mux_a=NULL;
350 muxer_stream_t* mux_v=NULL;
351 off_t muxer_f_size=0;
353 #ifdef HAVE_MP3LAME
354 lame_global_flags *lame;
355 #endif
357 double v_pts_corr=0;
358 double v_timer_corr=0;
360 m_entry_t* filelist = NULL;
361 char* filename=NULL;
362 char* frameno_filename="frameno.avi";
364 int decoded_frameno=0;
365 int next_frameno=-1;
367 unsigned int timer_start;
369 mp_msg_init();
370 mp_msg_set_level(MSGL_STATUS);
371 mp_msg(MSGT_CPLAYER,MSGL_INFO, "MEncoder " VERSION " (C) 2000-2003 MPlayer Team\n\n");
373 /* Test for cpu capabilities (and corresponding OS support) for optimizing */
374 GetCpuCaps(&gCpuCaps);
375 #ifdef ARCH_X86
376 mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: Type: %d MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
377 gCpuCaps.cpuType,gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
378 gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
379 gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
380 #ifdef RUNTIME_CPUDETECT
381 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
382 #else
383 mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
384 #ifdef HAVE_MMX
385 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
386 #endif
387 #ifdef HAVE_MMX2
388 mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
389 #endif
390 #ifdef HAVE_3DNOW
391 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
392 #endif
393 #ifdef HAVE_3DNOWEX
394 mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
395 #endif
396 #ifdef HAVE_SSE
397 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
398 #endif
399 #ifdef HAVE_SSE2
400 mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
401 #endif
402 mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n\n");
403 #endif
404 #endif
406 InitTimer();
408 // check codec.conf
409 if(!parse_codec_cfg(get_path("codecs.conf"))){
410 if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
411 if(!parse_codec_cfg(NULL)){
412 mp_msg(MSGT_MENCODER,MSGL_HINT,MSGTR_CopyCodecsConf);
413 mencoder_exit(1,NULL);
415 mp_msg(MSGT_MENCODER,MSGL_INFO,MSGTR_BuiltinCodecsConf);
419 // FIXME: get rid of -dvd and other tricky options
420 stream2=open_stream(frameno_filename,0,&i);
421 if(stream2){
422 demuxer2=demux_open(stream2,DEMUXER_TYPE_AVI,-1,-1,-2,NULL);
423 if(demuxer2) printf(MSGTR_UsingPass3ControllFile,frameno_filename);
424 else mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized);
427 mconfig = m_config_new();
428 m_config_register_options(mconfig,mencoder_opts);
429 parse_cfgfiles(mconfig);
430 filelist = m_config_parse_me_command_line(mconfig, argc, argv);
431 if(!filelist) mencoder_exit(1, "error parsing cmdline");
432 m_entry_set_options(mconfig,&filelist[0]);
433 filename = filelist[0].name;
435 if(!filename){
436 printf(MSGTR_MissingFilename);
437 mencoder_exit(1,NULL);
440 mp_msg_set_level(verbose+MSGL_STATUS);
442 // check font
443 #ifdef USE_OSD
444 #ifdef HAVE_FREETYPE
445 init_freetype();
446 #else
447 if(font_name){
448 vo_font=read_font_desc(font_name,font_factor,verbose>1);
449 if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,font_name);
450 } else {
451 // try default:
452 vo_font=read_font_desc(get_path("font/font.desc"),font_factor,verbose>1);
453 if(!vo_font)
454 vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
456 #endif
457 #endif
459 vo_init_osd();
461 stream=open_stream(filename,0,&file_format);
463 if(!stream){
464 printf(MSGTR_CannotOpenFile_Device);
465 mencoder_exit(1,NULL);
468 printf("success: format: %d data: 0x%X - 0x%X\n",file_format, (int)(stream->start_pos),(int)(stream->end_pos));
470 #ifdef USE_DVDREAD
471 if(stream->type==STREAMTYPE_DVD){
472 if(audio_lang && audio_id==-1) audio_id=dvd_aid_from_lang(stream,audio_lang);
473 if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(stream,dvdsub_lang);
475 #endif
477 stream->start_pos+=seek_to_byte;
479 #ifdef HAVE_LIBCSS
480 // current_module="libcss";
481 if (dvdimportkey) {
482 if (dvd_import_key(dvdimportkey)) {
483 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorDVDkey);
484 mencoder_exit(1,NULL);
486 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_CmdlineDVDkey);
488 if (dvd_auth_device) {
489 // if (dvd_auth(dvd_auth_device,f)) {
490 if (dvd_auth(dvd_auth_device,filename)) {
491 mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_ErrorDVDAuth);
492 mencoder_exit(1,NULL);
494 mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_DVDauthOk);
496 #endif
498 if(stream_cache_size>0) stream_enable_cache(stream,stream_cache_size*1024,0,0);
500 if(demuxer2) audio_id=-2; /* do NOT read audio packets... */
502 //demuxer=demux_open(stream,file_format,video_id,audio_id,dvdsub_id);
503 demuxer=demux_open(stream,file_format,audio_id,video_id,dvdsub_id,filename);
504 if(!demuxer){
505 mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized);
506 printf(MSGTR_CannotOpenDemuxer);
507 mencoder_exit(1,NULL);
510 d_audio=demuxer2 ? demuxer2->audio : demuxer->audio;
511 d_video=demuxer->video;
512 d_dvdsub=demuxer->sub;
513 sh_audio=d_audio->sh;
514 sh_video=d_video->sh;
516 if(!sh_video)
518 mp_msg(MSGT_CPLAYER,MSGL_FATAL,"Video stream is mandatory!\n");
519 mencoder_exit(1,NULL);
522 if(!video_read_properties(sh_video)){
523 printf(MSGTR_CannotReadVideoProperties);
524 mencoder_exit(1,NULL);
527 mp_msg(MSGT_MENCODER,MSGL_INFO,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
528 demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
529 sh_video->fps,sh_video->frametime
532 if(force_fps){
533 sh_video->fps=force_fps;
534 sh_video->frametime=1.0f/sh_video->fps;
535 mp_msg(MSGT_MENCODER,MSGL_INFO,"input fps will be interpreted as %5.2f instead\n", sh_video->fps);
538 if(sh_audio && out_audio_codec<0){
539 if(audio_id==-2)
540 mp_msg(MSGT_MENCODER,MSGL_ERR,"This demuxer doesn't support -nosound yet.\n");
541 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoAudioEncoderSelected);
542 mencoder_exit(1,NULL);
544 if(sh_video && out_video_codec<0){
545 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoVideoEncoderSelected);
546 mencoder_exit(1,NULL);
549 if(sh_audio && (out_audio_codec || seek_to_sec || !sh_audio->wf)){
550 // Go through the codec.conf and find the best codec...
551 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
552 if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
553 sh_audio=d_audio->sh=NULL; // failed to init :(
555 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
558 // set up video encoder:
560 if (vobsub_out) {
561 unsigned int palette[16], width, height;
562 unsigned char tmp[3] = { 0, 0, 0 };
563 if (spudec_ifo && vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, dvdsub_id, tmp) >= 0)
564 vobsub_writer = vobsub_out_open(vobsub_out, palette, sh_video->disp_w, sh_video->disp_h,
565 vobsub_out_id?vobsub_out_id:(char *)tmp, vobsub_out_index);
566 #ifdef USE_DVDREAD
567 if (vobsub_writer == NULL) {
568 char tmp[3];
569 if (vobsub_out_id == NULL && stream->type == STREAMTYPE_DVD) {
570 int i;
571 dvd_priv_t *dvd = (dvd_priv_t*)stream->priv;
572 for (i = 0; i < dvd->nr_of_subtitles; ++i)
573 if (dvd->subtitles[i].id == dvdsub_id) {
574 tmp[0] = (dvd->subtitles[i].language >> 8) & 0xff;
575 tmp[1] = dvd->subtitles[i].language & 0xff;
576 tmp[2] = 0;
577 vobsub_out_id = tmp;
578 break;
581 vobsub_writer=vobsub_out_open(vobsub_out, stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
582 sh_video->disp_w, sh_video->disp_h, vobsub_out_id, vobsub_out_index);
584 #endif
586 else {
587 if (spudec_ifo) {
588 unsigned int palette[16], width, height;
589 if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
590 vo_spudec=spudec_new_scaled(palette, sh_video->disp_w, sh_video->disp_h);
592 #ifdef USE_DVDREAD
593 if (vo_spudec==NULL) {
594 vo_spudec=spudec_new_scaled(stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
595 sh_video->disp_w, sh_video->disp_h);
597 #endif
600 #ifdef USE_SUB
601 // after reading video params we should load subtitles because
602 // we know fps so now we can adjust subtitles time to ~6 seconds AST
603 // check .sub
604 // current_module="read_subtitles_file";
605 if(sub_name && sub_name[0]){
606 subdata=sub_read_file(sub_name[0], sh_video->fps);
607 if(!subdata) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,sub_name[0]);
608 } else
609 if(sub_auto) { // auto load sub file ...
610 subdata=sub_read_file( filename ? sub_filenames( get_path("sub/"), filename )[0]
611 : "default.sub", sh_video->fps );
613 #endif
615 // Apply current settings for forced subs
616 spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
618 // set up output file:
619 muxer_f=fopen(out_filename,"wb");
620 if(!muxer_f) {
621 printf(MSGTR_CannotOpenOutputFile, out_filename);
622 mencoder_exit(1,NULL);
625 muxer=muxer_new_muxer(out_file_format,muxer_f);
627 // ============= VIDEO ===============
629 mux_v=muxer_new_stream(muxer,MUXER_TYPE_VIDEO);
631 mux_v->buffer_size=0x200000; // 2MB
632 mux_v->buffer=malloc(mux_v->buffer_size);
634 mux_v->source=sh_video;
636 mux_v->h.dwSampleSize=0; // VBR
637 mux_v->h.dwScale=10000;
638 mux_v->h.dwRate=mux_v->h.dwScale*(force_ofps?force_ofps:sh_video->fps);
640 mux_v->codec=out_video_codec;
642 mux_v->bih=NULL;
643 sh_video->codec=NULL;
644 sh_video->video_out=NULL;
645 sh_video->vfilter=NULL; // fixme!
647 switch(mux_v->codec){
648 case VCODEC_COPY:
649 if (sh_video->bih)
650 mux_v->bih=sh_video->bih;
651 else
653 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER));
654 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
655 mux_v->bih->biWidth=sh_video->disp_w;
656 mux_v->bih->biHeight=sh_video->disp_h;
657 mux_v->bih->biCompression=sh_video->format;
658 mux_v->bih->biPlanes=1;
659 mux_v->bih->biBitCount=24; // FIXME!!!
660 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
662 printf("videocodec: framecopy (%dx%d %dbpp fourcc=%x)\n",
663 mux_v->bih->biWidth, mux_v->bih->biHeight,
664 mux_v->bih->biBitCount, mux_v->bih->biCompression);
665 break;
666 case VCODEC_FRAMENO:
667 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER));
668 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
669 mux_v->bih->biWidth=sh_video->disp_w;
670 mux_v->bih->biHeight=sh_video->disp_h;
671 mux_v->bih->biPlanes=1;
672 mux_v->bih->biBitCount=24;
673 mux_v->bih->biCompression=mmioFOURCC('F','r','N','o');
674 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
675 break;
676 default:
678 switch(mux_v->codec){
679 case VCODEC_DIVX4:
680 sh_video->vfilter=vf_open_encoder(NULL,"divx4",(char *)mux_v); break;
681 case VCODEC_LIBAVCODEC:
682 sh_video->vfilter=vf_open_encoder(NULL,"lavc",(char *)mux_v); break;
683 case VCODEC_RAWRGB:
684 sh_video->vfilter=vf_open_encoder(NULL,"rawrgb",(char *)mux_v); break;
685 case VCODEC_VFW:
686 sh_video->vfilter=vf_open_encoder(NULL,"vfw",(char *)mux_v); break;
687 case VCODEC_LIBDV:
688 sh_video->vfilter=vf_open_encoder(NULL,"libdv",(char *)mux_v); break;
689 case VCODEC_XVID:
690 sh_video->vfilter=vf_open_encoder(NULL,"xvid",(char *)mux_v); break;
691 case VCODEC_QTVIDEO:
692 sh_video->vfilter=vf_open_encoder(NULL,"qtvideo",(char *)mux_v); break;
693 case VCODEC_NUV:
694 sh_video->vfilter=vf_open_encoder(NULL,"nuv",(char *)mux_v); break;
697 if(!mux_v->bih || !sh_video->vfilter){
698 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_EncoderOpenFailed);
699 mencoder_exit(1,NULL);
701 // append 'expand' filter, it fixes stride problems and renders osd:
702 if (auto_expand) {
703 char* vf_args[] = { "osd", "1", NULL };
704 sh_video->vfilter=vf_open_filter(sh_video->vfilter,"expand",vf_args);
706 sh_video->vfilter=append_filters(sh_video->vfilter);
708 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
709 init_best_video_codec(sh_video,video_codec_list,video_fm_list);
710 mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
711 if(!sh_video->inited) mencoder_exit(1,NULL);
715 /* force output fourcc to .. */
716 if ((force_fourcc != NULL) && (strlen(force_fourcc) >= 4))
718 mux_v->bih->biCompression = mmioFOURCC(force_fourcc[0], force_fourcc[1],
719 force_fourcc[2], force_fourcc[3]);
720 printf(MSGTR_ForcingOutputFourcc,
721 mux_v->bih->biCompression, (char *)&mux_v->bih->biCompression);
724 //if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
726 // ============= AUDIO ===============
727 if(sh_audio){
729 mux_a=muxer_new_stream(muxer,MUXER_TYPE_AUDIO);
731 mux_a->buffer_size=0x100000; //16384;
732 mux_a->buffer=malloc(mux_a->buffer_size);
734 mux_a->source=sh_audio;
736 mux_a->codec=out_audio_codec;
738 switch(mux_a->codec){
739 case ACODEC_COPY:
740 if (sh_audio->wf){
741 mux_a->wf=sh_audio->wf;
742 if(!sh_audio->i_bps) sh_audio->i_bps=mux_a->wf->nAvgBytesPerSec;
743 } else {
744 mux_a->wf = malloc(sizeof(WAVEFORMATEX));
745 mux_a->wf->nBlockAlign = 1; //mux_a->h.dwSampleSize;
746 mux_a->wf->wFormatTag = sh_audio->format;
747 mux_a->wf->nChannels = sh_audio->channels;
748 mux_a->wf->nSamplesPerSec = sh_audio->samplerate;
749 mux_a->wf->nAvgBytesPerSec=sh_audio->i_bps; //mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
750 mux_a->wf->wBitsPerSample = 16; // FIXME
751 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
753 if(sh_audio->audio.dwScale){
754 mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize;
755 mux_a->h.dwScale=sh_audio->audio.dwScale;
756 mux_a->h.dwRate=sh_audio->audio.dwRate;
757 // mux_a->h.dwStart=sh_audio->audio.dwStart;
758 } else {
759 mux_a->h.dwSampleSize=mux_a->wf->nBlockAlign;
760 mux_a->h.dwScale=mux_a->h.dwSampleSize;
761 mux_a->h.dwRate=mux_a->wf->nAvgBytesPerSec;
763 printf("audiocodec: framecopy (format=%x chans=%d rate=%ld bits=%d bps=%ld sample=%ld)\n",
764 mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
765 mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
766 break;
767 case ACODEC_PCM:
768 printf("CBR PCM audio selected\n");
769 mux_a->h.dwScale=1;
770 mux_a->h.dwRate=force_srate?force_srate:sh_audio->samplerate;
771 mux_a->wf=malloc(sizeof(WAVEFORMATEX));
772 mux_a->wf->wFormatTag=0x1; // PCM
773 mux_a->wf->nChannels=audio_output_channels?audio_output_channels:sh_audio->channels;
774 mux_a->h.dwSampleSize=2*mux_a->wf->nChannels;
775 mux_a->wf->nBlockAlign=mux_a->h.dwSampleSize;
776 mux_a->wf->nSamplesPerSec=mux_a->h.dwRate;
777 mux_a->wf->nAvgBytesPerSec=mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
778 mux_a->wf->wBitsPerSample=16;
779 mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
780 // setup filter:
781 if(!init_audio_filters(sh_audio,
782 sh_audio->samplerate,
783 sh_audio->channels, sh_audio->sample_format, sh_audio->samplesize,
784 mux_a->wf->nSamplesPerSec, mux_a->wf->nChannels,
785 (mux_a->wf->wBitsPerSample==8)? AFMT_U8:AFMT_S16_LE,
786 mux_a->wf->wBitsPerSample/8,
787 16384, mux_a->wf->nAvgBytesPerSec)){
788 mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format!\n");
790 break;
791 #ifdef HAVE_MP3LAME
792 case ACODEC_VBRMP3:
793 printf("MP3 audio selected\n");
794 mux_a->h.dwSampleSize=0; // VBR
795 mux_a->h.dwRate=force_srate?force_srate:sh_audio->samplerate;
796 mux_a->h.dwScale=(mux_a->h.dwRate<32000)?576:1152; // samples/frame
797 if(sizeof(MPEGLAYER3WAVEFORMAT)!=30) mp_msg(MSGT_MENCODER,MSGL_WARN,"sizeof(MPEGLAYER3WAVEFORMAT)==%d!=30, maybe broken C compiler?\n",sizeof(MPEGLAYER3WAVEFORMAT));
798 mux_a->wf=malloc(sizeof(MPEGLAYER3WAVEFORMAT)); // should be 30
799 mux_a->wf->wFormatTag=0x55; // MP3
800 mux_a->wf->nChannels= (lame_param_mode<0) ? sh_audio->channels :
801 ((lame_param_mode==3) ? 1 : 2);
802 mux_a->wf->nSamplesPerSec=mux_a->h.dwRate;
803 mux_a->wf->nAvgBytesPerSec=192000/8; // FIXME!
804 mux_a->wf->nBlockAlign=(mux_a->h.dwRate<32000)?576:1152; // required for l3codeca.acm + WMP 6.4
805 mux_a->wf->wBitsPerSample=0; //16;
806 // from NaNdub: (requires for l3codeca.acm)
807 mux_a->wf->cbSize=12;
808 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->wID=1;
809 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->fdwFlags=2;
810 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nBlockSize=(mux_a->h.dwRate<32000)?576:1152; // ???
811 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nFramesPerBlock=1;
812 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nCodecDelay=0;
813 // setup filter:
814 if(!init_audio_filters(sh_audio,
815 sh_audio->samplerate,
816 sh_audio->channels, sh_audio->sample_format, sh_audio->samplesize,
817 mux_a->wf->nSamplesPerSec, mux_a->wf->nChannels,
818 #ifdef WORDS_BIGENDIAN
819 AFMT_S16_BE, 2,
820 #else
821 AFMT_S16_LE, 2,
822 #endif
823 4608, mux_a->h.dwRate*mux_a->wf->nChannels*2)){
824 mp_msg(MSGT_CPLAYER,MSGL_ERR,"Couldn't find matching filter / ao format!\n");
826 break;
827 #endif
828 #ifdef USE_LIBAVCODEC
829 case ACODEC_LAVC:
830 if(!lavc_param_acodec)
832 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Audio LAVC, Missing codec name!\n");
833 exit(1);
836 if(!avcodec_inited){
837 avcodec_init();
838 avcodec_register_all();
839 avcodec_inited=1;
842 lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec);
843 if (!lavc_acodec)
845 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Audio LAVC, couldn't find encoder for codec %s\n", lavc_param_acodec);
846 exit(1);
849 lavc_actx = avcodec_alloc_context();
850 if(lavc_actx == NULL)
852 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Audio LAVC, couldn't allocate context!\n");
853 exit(1);
856 if(lavc_param_atag == 0)
857 lavc_param_atag = lavc_find_atag(lavc_param_acodec);
859 // put sample parameters
860 lavc_actx->channels = audio_output_channels ? audio_output_channels : sh_audio->channels;
861 lavc_actx->sample_rate = force_srate ? force_srate : sh_audio->samplerate;
862 lavc_actx->bit_rate = lavc_param_abitrate * 1000;
865 * Special case for imaadpcm.
866 * The bitrate is only dependant on samplerate.
867 * We have to known frame_size and block_align in advance,
868 * so I just copied the code from libavcodec/adpcm.c
870 * However, ms imaadpcm uses a block_align of 2048,
871 * lavc defaults to 1024
873 if(lavc_param_atag == 0x11) {
874 int blkalign = 2048;
875 int framesize = (blkalign - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1;
876 lavc_actx->bit_rate = lavc_actx->sample_rate*8*blkalign/framesize;
879 if(avcodec_open(lavc_actx, lavc_acodec) < 0)
881 mp_msg(MSGT_MENCODER, MSGL_FATAL, "Couldn't open codec %s, br=%d\n", lavc_param_acodec, lavc_param_abitrate);
882 exit(1);
885 if(lavc_param_atag == 0x11) {
886 lavc_actx->block_align = 2048;
887 lavc_actx->frame_size = (lavc_actx->block_align - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1;
890 lavc_abuf = malloc(lavc_actx->frame_size * 2 * lavc_actx->channels);
891 if(lavc_abuf == NULL)
893 fprintf(stderr, "Couldn't allocate %d bytes\n", lavc_actx->frame_size * 2 * lavc_actx->channels);
894 exit(1);
897 if (sizeof(MPEGLAYER3WAVEFORMAT) != 30) // should be 30
898 broken_c_compiler___size_of_MPEGLAYER3WAVEFORMAT_not_30();
900 mux_a->wf = malloc(sizeof(MPEGLAYER3WAVEFORMAT));
901 mux_a->wf->wFormatTag = lavc_param_atag;
902 mux_a->wf->nChannels = lavc_actx->channels;
903 mux_a->wf->nSamplesPerSec = lavc_actx->sample_rate;
904 mux_a->wf->nAvgBytesPerSec = (lavc_actx->bit_rate / 8);
905 mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
906 if (lavc_actx->block_align) {
907 mux_a->h.dwSampleSize = mux_a->h.dwScale = lavc_actx->block_align;
908 } else {
909 mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * lavc_actx->frame_size)/ mux_a->wf->nSamplesPerSec; /* for cbr */
911 if ((mux_a->wf->nAvgBytesPerSec *
912 lavc_actx->frame_size) % mux_a->wf->nSamplesPerSec) {
913 mux_a->h.dwScale = lavc_actx->frame_size;
914 mux_a->h.dwRate = lavc_actx->sample_rate;
915 mux_a->h.dwSampleSize = 0; // Blocksize not constant
916 } else {
917 mux_a->h.dwSampleSize = mux_a->h.dwScale;
920 mux_a->wf->nBlockAlign = mux_a->h.dwScale;
921 mux_a->h.dwSuggestedBufferSize = audio_preload*mux_a->wf->nAvgBytesPerSec;
922 mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
924 switch (lavc_param_atag) {
925 case 0x11: /* imaadpcm */
926 mux_a->wf->wBitsPerSample = 4;
927 mux_a->wf->cbSize = 2;
929 * Magic imaadpcm values, currently probably only valid
930 * for 48KHz Stereo
932 ((unsigned char*)mux_a->wf)[sizeof(WAVEFORMATEX)] = 0xf9;
933 ((unsigned char*)mux_a->wf)[sizeof(WAVEFORMATEX)+1] = 0x07;
934 break;
935 case 0x55: /* mp3 */
936 mux_a->wf->cbSize = 12;
937 mux_a->wf->wBitsPerSample = 0; /* does not apply */
938 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1;
939 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2;
940 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign;
941 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1;
942 ((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0;
943 break;
944 default:
945 mux_a->wf->cbSize = 0;
946 mux_a->wf->wBitsPerSample = 0; /* Unknown */
947 break;
950 // setup filter:
951 if (!init_audio_filters(
952 sh_audio,
953 sh_audio->samplerate, sh_audio->channels,
954 sh_audio->sample_format, sh_audio->samplesize,
955 mux_a->wf->nSamplesPerSec, mux_a->wf->nChannels,
956 AFMT_S16_NE, 2,
957 mux_a->h.dwSuggestedBufferSize,
958 mux_a->h.dwSuggestedBufferSize*2)) {
959 mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't find matching filter / ao format!\n");
960 exit(1);
963 mp_msg(MSGT_MENCODER, MSGL_V, "FRAME_SIZE: %d, BUFFER_SIZE: %d, TAG: 0x%x\n", lavc_actx->frame_size, lavc_actx->frame_size * 2 * lavc_actx->channels, mux_a->wf->wFormatTag);
965 break;
966 #endif
969 if (verbose>1) print_wave_header(mux_a->wf);
971 if(audio_delay!=0.0){
972 mux_a->h.dwStart=audio_delay*mux_a->h.dwRate/mux_a->h.dwScale;
973 printf("Setting AUDIO DELAY to %5.3f\n",mux_a->h.dwStart*mux_a->h.dwScale/(float)mux_a->h.dwRate);
976 } // if(sh_audio)
978 printf(MSGTR_WritingAVIHeader);
979 if (muxer->cont_write_header) muxer_write_header(muxer);
981 decoded_frameno=0;
983 if(sh_audio)
984 switch(mux_a->codec){
985 #ifdef HAVE_MP3LAME
986 case ACODEC_VBRMP3:
988 lame=lame_init();
989 lame_set_bWriteVbrTag(lame,0);
990 lame_set_in_samplerate(lame,mux_a->wf->nSamplesPerSec);
991 //lame_set_in_samplerate(lame,sh_audio->samplerate); // if resampling done by lame
992 lame_set_num_channels(lame,mux_a->wf->nChannels);
993 lame_set_out_samplerate(lame,mux_a->wf->nSamplesPerSec);
994 lame_set_quality(lame,lame_param_algqual); // 0 = best q
995 if(lame_param_vbr){ // VBR:
996 lame_set_VBR(lame,lame_param_vbr); // vbr mode
997 lame_set_VBR_q(lame,lame_param_quality+1); // 1 = best vbr q 6=~128k
998 if(lame_param_br>0) lame_set_VBR_mean_bitrate_kbps(lame,lame_param_br);
999 } else { // CBR:
1000 if(lame_param_br>0) lame_set_brate(lame,lame_param_br);
1002 if(lame_param_mode>=0) lame_set_mode(lame,lame_param_mode); // j-st
1003 if(lame_param_ratio>0) lame_set_compression_ratio(lame,lame_param_ratio);
1004 if(lame_param_scale>0) {
1005 printf("Setting audio input gain to %f\n", lame_param_scale);
1006 lame_set_scale(lame,lame_param_scale);
1008 #if HAVE_MP3LAME >= 392
1009 if(lame_param_preset != NULL){
1010 printf ("\npreset=%s\n\n",lame_param_preset);
1011 lame_presets_set(lame,lame_param_fast, (lame_param_vbr==0), lame_param_preset);
1013 #endif
1014 lame_init_params(lame);
1015 if(verbose>0){
1016 lame_print_config(lame);
1017 lame_print_internals(lame);
1019 break;
1020 #endif
1023 signal(SIGINT,exit_sighandler); // Interrupt from keyboard
1024 signal(SIGQUIT,exit_sighandler); // Quit from keyboard
1025 signal(SIGTERM,exit_sighandler); // kill
1027 timer_start=GetTimerMS();
1029 if (seek_to_sec) {
1030 int a,b; float d;
1032 if (sscanf(seek_to_sec, "%d:%d:%f", &a,&b,&d)==3)
1033 d += 3600*a + 60*b;
1034 else if (sscanf(seek_to_sec, "%d:%f", &a, &d)==2)
1035 d += 60*a;
1036 else
1037 sscanf(seek_to_sec, "%f", &d);
1039 demux_seek(demuxer, d, 1);
1040 // there is 2 way to handle the -ss option in 3-pass mode:
1041 // > 1. do the first pass for the whole file, and use -ss for 2nd/3rd pases only
1042 // > 2. do all the 3 passes with the same -ss value
1043 // this line enables behaviour 1. (and kills 2. at the same time):
1044 // if(demuxer2) demux_seek(demuxer2, d, 1);
1047 if (out_file_format == MUXER_TYPE_MPEG)
1049 if (audio_preload > 0.4) {
1050 fprintf(stderr,"Limiting audio preload to 0.4s\n");
1051 audio_preload = 0.4;
1053 if (audio_density < 4) {
1054 fprintf(stderr,"Increasing audio density to 4\n");
1055 audio_density = 4;
1059 if(file_format == DEMUXER_TYPE_TV)
1061 fprintf(stderr,"Forcing audio preload to 0, max pts correction to 0\n");
1062 audio_preload = 0.0;
1063 default_max_pts_correction = 0;
1066 play_n_frames=play_n_frames_mf;
1068 while(!at_eof){
1070 float frame_time=0;
1071 int blit_frame=0;
1072 float a_pts=0;
1073 float v_pts=0;
1074 unsigned char* start=NULL;
1075 int in_size;
1076 int skip_flag=0; // 1=skip -1=duplicate
1078 if((end_at_type == END_AT_SIZE && end_at <= ftello(muxer_f)) ||
1079 (end_at_type == END_AT_TIME && end_at < sh_video->timer))
1080 break;
1082 if(play_n_frames>=0){
1083 --play_n_frames;
1084 if(play_n_frames<0) break;
1087 if(sh_audio){
1088 // get audio:
1089 while(mux_a->timer-audio_preload<mux_v->timer){
1090 int len=0;
1092 ptimer_start = GetTimerMS();
1094 #ifdef USE_LIBAVCODEC
1095 if(mux_a->codec == ACODEC_LAVC){
1096 int size, rd_len;
1098 size = lavc_actx->frame_size * 2 * mux_a->wf->nChannels;
1100 rd_len = dec_audio(sh_audio, lavc_abuf, size);
1101 if(rd_len != size)
1102 break;
1104 // Encode one frame
1105 mux_a->buffer_len += avcodec_encode_audio(lavc_actx, mux_a->buffer + mux_a->buffer_len, size, lavc_abuf);
1106 if (mux_a->h.dwSampleSize) { /* CBR */
1108 * work around peculiar lame behaviour
1110 if (mux_a->buffer_len < mux_a->wf->nBlockAlign) {
1111 len = 0;
1112 } else {
1113 len = mux_a->wf->nBlockAlign*(mux_a->buffer_len/mux_a->wf->nBlockAlign);
1115 } else { /* VBR */
1116 len = mux_a->buffer_len;
1118 if (mux_v->timer == 0) mux_a->h.dwInitialFrames++;
1120 #endif
1121 if(mux_a->h.dwSampleSize){
1122 // CBR - copy 0.5 sec of audio
1123 switch(mux_a->codec){
1124 case ACODEC_COPY: // copy
1125 len=mux_a->wf->nAvgBytesPerSec/audio_density;
1126 len/=mux_a->h.dwSampleSize;if(len<1) len=1;
1127 len*=mux_a->h.dwSampleSize;
1128 len=demux_read_data(sh_audio->ds,mux_a->buffer,len);
1129 break;
1130 case ACODEC_PCM:
1131 len=mux_a->h.dwSampleSize*(mux_a->h.dwRate/audio_density);
1132 len=dec_audio(sh_audio,mux_a->buffer,len);
1133 break;
1135 } else {
1136 // VBR - encode/copy an audio frame
1137 switch(mux_a->codec){
1138 case ACODEC_COPY: // copy
1139 len=ds_get_packet(sh_audio->ds,(unsigned char**) &mux_a->buffer);
1140 // printf("VBR audio framecopy not yet implemented!\n");
1141 break;
1142 #ifdef HAVE_MP3LAME
1143 case ACODEC_VBRMP3:
1144 while(mux_a->buffer_len<4){
1145 unsigned char tmp[2304];
1146 int len=dec_audio(sh_audio,tmp,2304);
1147 if(len<=0) break; // eof
1148 /* mono encoding, a bit tricky */
1149 if (mux_a->wf->nChannels == 1)
1151 len = lame_encode_buffer(lame, (short *)tmp, (short *)tmp, len/2,
1152 mux_a->buffer+mux_a->buffer_len, mux_a->buffer_size-mux_a->buffer_len);
1154 else
1156 len=lame_encode_buffer_interleaved(lame,
1157 (short *)tmp,len/4,
1158 mux_a->buffer+mux_a->buffer_len,mux_a->buffer_size-mux_a->buffer_len);
1160 if(len<0) break; // error
1161 mux_a->buffer_len+=len;
1163 if(mux_a->buffer_len<4) break;
1164 len=mp_decode_mp3_header(mux_a->buffer);
1165 //printf("%d\n",len);
1166 if(len<=0) break; // bad frame!
1167 // printf("[%d]\n",mp_mp3_get_lsf(mux_a->buffer));
1168 while(mux_a->buffer_len<len){
1169 unsigned char tmp[2304];
1170 int len=dec_audio(sh_audio,tmp,2304);
1171 if(len<=0) break; // eof
1172 /* mono encoding, a bit tricky */
1173 if (mux_a->wf->nChannels == 1)
1175 len = lame_encode_buffer(lame, (short *)tmp, (short *)tmp, len/2,
1176 mux_a->buffer+mux_a->buffer_len, mux_a->buffer_size-mux_a->buffer_len);
1178 else
1180 len=lame_encode_buffer_interleaved(lame,
1181 (short *)tmp,len/4,
1182 mux_a->buffer+mux_a->buffer_len,mux_a->buffer_size-mux_a->buffer_len);
1184 if(len<0) break; // error
1185 mux_a->buffer_len+=len;
1187 break;
1188 #endif
1191 if(len<=0) break; // EOF?
1192 muxer_write_chunk(mux_a,len,0x10);
1193 if(!mux_a->h.dwSampleSize && mux_a->timer>0)
1194 mux_a->wf->nAvgBytesPerSec=0.5f+(double)mux_a->size/mux_a->timer; // avg bps (VBR)
1195 if(mux_a->buffer_len>=len){
1196 mux_a->buffer_len-=len;
1197 memcpy(mux_a->buffer,mux_a->buffer+len,mux_a->buffer_len);
1201 audiosamples++;
1202 audiorate+= (GetTimerMS() - ptimer_start);
1206 // get video frame!
1208 in_size=video_read_frame(sh_video,&frame_time,&start,force_fps);
1209 if(in_size<0){ at_eof=1; break; }
1210 sh_video->timer+=frame_time; ++decoded_frameno;
1212 v_timer_corr-=frame_time-(float)mux_v->h.dwScale/mux_v->h.dwRate;
1214 if(demuxer2){ // 3-pass encoding, read control file (frameno.avi)
1215 // find our frame:
1216 while(next_frameno<decoded_frameno){
1217 int* start;
1218 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1219 if(len<0){ at_eof=1;break;}
1220 if(len==0) --skip_flag; else // duplicate
1221 if(len==4) next_frameno=start[0];
1223 if(at_eof) break;
1224 // if(skip_flag) printf("!!!!!!!!!!!!\n");
1225 skip_flag=next_frameno-decoded_frameno;
1226 // find next frame:
1227 while(next_frameno<=decoded_frameno){
1228 int* start;
1229 int len=ds_get_packet(demuxer2->video,(unsigned char**) &start);
1230 if(len<0){ at_eof=1;break;}
1231 if(len==0) --skip_flag; else // duplicate
1232 if(len==4) next_frameno=start[0];
1234 // if(at_eof) break;
1235 // printf("Current fno=%d requested=%d skip=%d \n",decoded_frameno,fno,skip_flag);
1236 } else {
1238 // check frame duplicate/drop:
1240 //printf("\r### %5.3f ###\n",v_timer_corr);
1242 if(v_timer_corr>=(float)mux_v->h.dwScale/mux_v->h.dwRate &&
1243 (skip_limit<0 || skip_flag<skip_limit) ){
1244 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1245 ++skip_flag; // skip
1246 } else
1247 while(v_timer_corr<=-(float)mux_v->h.dwScale/mux_v->h.dwRate &&
1248 (skip_limit<0 || (-skip_flag)<skip_limit) ){
1249 v_timer_corr+=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1250 --skip_flag; // dup
1253 while( (v_pts_corr<=-(float)mux_v->h.dwScale/mux_v->h.dwRate && skip_flag>0)
1254 || (v_pts_corr<=-2*(float)mux_v->h.dwScale/mux_v->h.dwRate) ){
1255 v_pts_corr+=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1256 --skip_flag; // dup
1258 if( (v_pts_corr>=(float)mux_v->h.dwScale/mux_v->h.dwRate && skip_flag<0)
1259 || (v_pts_corr>=2*(float)mux_v->h.dwScale/mux_v->h.dwRate) )
1260 if(skip_flag<=0){ // we can't skip more than 1 frame now
1261 v_pts_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1262 ++skip_flag; // skip
1265 } // demuxer2
1267 ptimer_start = GetTimerMS();
1269 switch(mux_v->codec){
1270 case VCODEC_COPY:
1271 mux_v->buffer=start;
1272 if(skip_flag<=0) muxer_write_chunk(mux_v,in_size,(sh_video->ds->flags&1)?0x10:0);
1273 break;
1274 case VCODEC_FRAMENO:
1275 mux_v->buffer=(unsigned char *)&decoded_frameno; // tricky
1276 if(skip_flag<=0) muxer_write_chunk(mux_v,sizeof(int),0x10);
1277 break;
1278 default:
1279 // decode_video will callback down to ve_*.c encoders, through the video filters
1280 blit_frame=decode_video(sh_video,start,in_size,(skip_flag>0)?1:0);
1281 if(!blit_frame){
1282 badframes++;
1283 if(skip_flag<=0){
1284 // unwanted skipping of a frame, what to do?
1285 if(skip_limit==0){
1286 // skipping not allowed -> write empty frame:
1287 muxer_write_chunk(mux_v,0,0);
1288 } else {
1289 // skipping allowed -> skip it and distriubute timer error:
1290 v_timer_corr-=(float)mux_v->h.dwScale/mux_v->h.dwRate;
1296 videosamples++;
1297 videorate+=(GetTimerMS() - ptimer_start);
1299 if(skip_flag<0){
1300 // duplicate frame
1301 if(file_format != DEMUXER_TYPE_TV && !verbose) printf(MSGTR_DuplicateFrames,-skip_flag);
1302 while(skip_flag<0){
1303 duplicatedframes++;
1304 muxer_write_chunk(mux_v,0,0);
1305 ++skip_flag;
1307 } else
1308 if(skip_flag>0){
1309 // skip frame
1310 if(file_format != DEMUXER_TYPE_TV && !verbose) printf(MSGTR_SkipFrame);
1311 skippedframes++;
1312 --skip_flag;
1315 if(sh_audio && !demuxer2){
1316 float AV_delay,x;
1317 // A-V sync!
1318 #if 0
1319 if(pts_from_bps){
1320 unsigned int samples=(sh_audio->audio.dwSampleSize)?
1321 ((ds_tell(d_audio)-sh_audio->a_in_buffer_len)/sh_audio->audio.dwSampleSize) :
1322 (d_audio->block_no); // <- used for VBR audio
1323 // printf("samples=%d \n",samples);
1324 a_pts=samples*(float)sh_audio->audio.dwScale/(float)sh_audio->audio.dwRate;
1325 delay_corrected=1;
1326 } else
1327 #endif
1329 // PTS = (last timestamp) + (bytes after last timestamp)/(bytes per sec)
1330 a_pts=d_audio->pts;
1331 if(!delay_corrected) if(a_pts) delay_corrected=1;
1332 //printf("*** %5.3f ***\n",a_pts);
1333 a_pts+=(ds_tell_pts(d_audio)-sh_audio->a_in_buffer_len)/(float)sh_audio->i_bps;
1335 v_pts=sh_video ? sh_video->pts : d_video->pts;
1336 // av = compensated (with out buffering delay) A-V diff
1337 AV_delay=(a_pts-v_pts); AV_delay-=mux_a->timer-(mux_v->timer-(v_timer_corr+v_pts_corr));
1338 // compensate input video timer by av:
1339 x=AV_delay*0.1f;
1340 if(x<-max_pts_correction) x=-max_pts_correction; else
1341 if(x> max_pts_correction) x= max_pts_correction;
1342 if(default_max_pts_correction>=0)
1343 max_pts_correction=default_max_pts_correction;
1344 else
1345 max_pts_correction=sh_video->frametime*0.10; // +-10% of time
1346 // sh_video->timer-=x;
1347 c_total+=x;
1348 v_pts_corr+=x;
1351 // printf("A:%6.1f V:%6.1f A-V:%7.3f oAV:%7.3f diff:%7.3f ct:%7.3f vpc:%7.3f \r",
1352 // a_pts,v_pts,a_pts-v_pts,
1353 // (float)(mux_a->timer-mux_v->timer),
1354 // AV_delay, c_total, v_pts_corr );
1355 // printf("V:%6.1f \r", d_video->pts );
1357 #if 0
1358 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",
1359 a_pts,v_pts,a_pts-v_pts,c_total,
1360 (int)sh_video->num_frames,(int)sh_video->num_frames_decoded,
1361 (sh_video->timer>0.5)?(int)(100.0*video_time_usage/(double)sh_video->timer):0,
1362 (sh_video->timer>0.5)?(int)(100.0*vout_time_usage/(double)sh_video->timer):0,
1363 (sh_video->timer>0.5)?(100.0*audio_time_usage/(double)sh_video->timer):0
1364 ,cache_fill_status
1366 #endif
1368 { float t=(GetTimerMS()-timer_start)*0.001f;
1369 float len=(demuxer->movi_end-demuxer->movi_start);
1370 float p=len>1000 ? (float)(demuxer->filepos-demuxer->movi_start) / len : 0;
1371 #if 0
1372 if(!len && sh_audio && sh_audio->audio.dwLength>100){
1373 p=(sh_audio->audio.dwSampleSize? ds_tell(sh_audio->ds)/sh_audio->audio.dwSampleSize : sh_audio->ds->block_no)
1374 / (float)(sh_audio->audio.dwLength);
1376 #endif
1377 #if 0
1378 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"%d < %d < %d \r",
1379 (int)demuxer->movi_start,
1380 (int)demuxer->filepos,
1381 (int)demuxer->movi_end);
1382 #else
1383 if(verbose>0) {
1384 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",
1385 mux_v->timer, decoded_frameno, (int)(p*100),
1386 (t>1) ? (int)(decoded_frameno/t+0.5) : 0,
1387 (p>0.001) ? (int)((t/p-t)/60) : 0,
1388 (p>0.001) ? (int)(ftello(muxer_f)/p/1024/1024) : 0,
1389 v_pts_corr,
1390 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1391 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0,
1392 audiorate/audiosamples, videorate/videosamples,
1393 duplicatedframes, badframes, skippedframes
1395 } else
1396 mp_msg(MSGT_AVSYNC,MSGL_STATUS,"Pos:%6.1fs %6df (%2d%%) %3dfps Trem:%4dmin %3dmb A-V:%5.3f [%d:%d]\r",
1397 mux_v->timer, decoded_frameno, (int)(p*100),
1398 (t>1) ? (int)(decoded_frameno/t+0.5) : 0,
1399 (p>0.001) ? (int)((t/p-t)/60) : 0,
1400 (p>0.001) ? (int)(ftello(muxer_f)/p/1024/1024) : 0,
1401 v_pts_corr,
1402 (mux_v->timer>1) ? (int)(mux_v->size/mux_v->timer/125) : 0,
1403 (mux_a && mux_a->timer>1) ? (int)(mux_a->size/mux_a->timer/125) : 0
1405 #endif
1407 fflush(stdout);
1409 #ifdef USE_SUB
1410 // find sub
1411 if(subdata && sh_video->pts>0){
1412 float pts=sh_video->pts;
1413 if(sub_fps==0) sub_fps=sh_video->fps;
1414 if (pts > sub_last_pts || pts < sub_last_pts-1.0 ) {
1415 find_sub(subdata, (pts+sub_delay) *
1416 (subdata->sub_uses_time? 100. : sub_fps));
1417 // FIXME! frame counter...
1418 sub_last_pts = pts;
1421 #endif
1423 #ifdef USE_DVDREAD
1424 // DVD sub:
1425 if(vo_spudec||vobsub_writer){
1426 unsigned char* packet=NULL;
1427 int len;
1428 while((len=ds_get_packet_sub(d_dvdsub,&packet))>0){
1429 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);
1430 if (vo_spudec)
1431 spudec_assemble(vo_spudec,packet,len,90000*d_dvdsub->pts);
1432 if (vobsub_writer)
1433 vobsub_out_output(vobsub_writer,packet,len,mux_v->timer + d_dvdsub->pts - sh_video->pts);
1435 if (vo_spudec) {
1436 spudec_heartbeat(vo_spudec,90000*sh_video->pts);
1437 vo_osd_changed(OSDTYPE_SPU);
1440 #endif
1442 if(ferror(muxer_f)) {
1443 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile, out_filename);
1444 mencoder_exit(1, NULL);
1447 } // while(!at_eof)
1449 #ifdef HAVE_MP3LAME
1450 // fixup CBR mp3 audio header:
1451 if(sh_audio && mux_a->codec==ACODEC_VBRMP3 && !lame_param_vbr){
1452 mux_a->h.dwSampleSize=1;
1453 ((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nBlockSize=
1454 (mux_a->size+(mux_a->h.dwLength>>1))/mux_a->h.dwLength;
1455 mux_a->h.dwLength=mux_a->size;
1456 mux_a->h.dwRate=mux_a->wf->nAvgBytesPerSec;
1457 mux_a->h.dwScale=1;
1458 mux_a->wf->nBlockAlign=1;
1459 printf("\n\nCBR audio: %ld bytes/sec, %d bytes/block\n",
1460 mux_a->h.dwRate,((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nBlockSize);
1462 #endif
1464 printf(MSGTR_WritingAVIIndex);
1465 if (muxer->cont_write_index) muxer_write_index(muxer);
1466 muxer_f_size=ftello(muxer_f);
1467 printf(MSGTR_FixupAVIHeader);
1468 fseek(muxer_f,0,SEEK_SET);
1469 if (muxer->cont_write_header) muxer_write_header(muxer); // update header
1470 if(ferror(muxer_f) || fclose(muxer_f) != 0) {
1471 mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_ErrorWritingFile, out_filename);
1472 mencoder_exit(1, NULL);
1474 if(vobsub_writer)
1475 vobsub_out_close(vobsub_writer);
1477 if(out_video_codec==VCODEC_FRAMENO && mux_v->timer>100){
1478 printf(MSGTR_RecommendedVideoBitrate,"650MB",(int)((650*1024*1024-muxer_f_size)/mux_v->timer/125));
1479 printf(MSGTR_RecommendedVideoBitrate,"700MB",(int)((700*1024*1024-muxer_f_size)/mux_v->timer/125));
1480 printf(MSGTR_RecommendedVideoBitrate,"800MB",(int)((800*1024*1024-muxer_f_size)/mux_v->timer/125));
1481 printf(MSGTR_RecommendedVideoBitrate,"2 x 650MB",(int)((2*650*1024*1024-muxer_f_size)/mux_v->timer/125));
1482 printf(MSGTR_RecommendedVideoBitrate,"2 x 700MB",(int)((2*700*1024*1024-muxer_f_size)/mux_v->timer/125));
1483 printf(MSGTR_RecommendedVideoBitrate,"2 x 800MB",(int)((2*800*1024*1024-muxer_f_size)/mux_v->timer/125));
1486 printf(MSGTR_VideoStreamResult,
1487 (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);
1488 if(sh_audio)
1489 printf(MSGTR_AudioStreamResult,
1490 (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);
1492 if(sh_video){ uninit_video(sh_video);sh_video=NULL; }
1493 if(demuxer) free_demuxer(demuxer);
1494 if(stream) free_stream(stream); // kill cache thread
1496 #ifdef USE_LIBAVCODEC
1497 if(lavc_abuf != NULL)
1498 free(lavc_abuf);
1499 #endif
1501 return interrupted;
1504 static int parse_end_at(m_option_t *conf, const char* param)
1507 end_at_type = END_AT_NONE;
1509 /* End at size parsing */
1511 char unit[4];
1513 end_at_type = END_AT_SIZE;
1515 if(sscanf(param, "%lf%3s", &end_at, unit) == 2) {
1516 if(!strcasecmp(unit, "b"))
1518 else if(!strcasecmp(unit, "kb"))
1519 end_at *= 1024;
1520 else if(!strcasecmp(unit, "mb"))
1521 end_at *= 1024*1024;
1522 else
1523 end_at_type = END_AT_NONE;
1525 else
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)
1533 int a,b; float d;
1535 end_at_type = END_AT_TIME;
1537 if (sscanf(param, "%d:%d:%f", &a, &b, &d) == 3)
1538 end_at = 3600*a + 60*b + d;
1539 else if (sscanf(param, "%d:%f", &a, &d) == 2)
1540 end_at = 60*a + d;
1541 else if (sscanf(param, "%f", &d) == 1)
1542 end_at = d;
1543 else
1544 end_at_type = END_AT_NONE;
1547 if(end_at_type == END_AT_NONE)
1548 return ERR_FUNC_ERR;
1550 return 1;
1553 #if 0
1554 /* Flip the image in src and store the result in dst. src and dst may overlap.
1555 width is the size of each line in bytes. */
1556 static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width,
1557 int height)
1559 uint8_t* tmp = malloc(width);
1560 int i;
1562 for(i = 0; i < height/2; i++) {
1563 memcpy(tmp, &src[i*width], width);
1564 memcpy(&dst[i * width], &src[(height - i) * width], width);
1565 memcpy(&dst[(height - i) * width], tmp, width);
1568 free(tmp);
1569 return dst;
1571 #endif
1573 #if HAVE_MP3LAME >= 392
1574 /* lame_presets_set
1575 taken out of presets_set in lame-3.93.1/frontend/parse.c and modified */
1576 static int lame_presets_set( lame_t gfp, int fast, int cbr, const char* preset_name )
1578 int mono = 0;
1580 if (strcmp(preset_name, "help") == 0) {
1581 fprintf(stdout, "LAME version %s (%s)\n\n", get_lame_version(), get_lame_url());
1582 lame_presets_longinfo_dm( stdout );
1583 return -1;
1588 //aliases for compatibility with old presets
1590 if (strcmp(preset_name, "phone") == 0) {
1591 preset_name = "16";
1592 mono = 1;
1594 if ( (strcmp(preset_name, "phon+") == 0) ||
1595 (strcmp(preset_name, "lw") == 0) ||
1596 (strcmp(preset_name, "mw-eu") == 0) ||
1597 (strcmp(preset_name, "sw") == 0)) {
1598 preset_name = "24";
1599 mono = 1;
1601 if (strcmp(preset_name, "mw-us") == 0) {
1602 preset_name = "40";
1603 mono = 1;
1605 if (strcmp(preset_name, "voice") == 0) {
1606 preset_name = "56";
1607 mono = 1;
1609 if (strcmp(preset_name, "fm") == 0) {
1610 preset_name = "112";
1612 if ( (strcmp(preset_name, "radio") == 0) ||
1613 (strcmp(preset_name, "tape") == 0)) {
1614 preset_name = "112";
1616 if (strcmp(preset_name, "hifi") == 0) {
1617 preset_name = "160";
1619 if (strcmp(preset_name, "cd") == 0) {
1620 preset_name = "192";
1622 if (strcmp(preset_name, "studio") == 0) {
1623 preset_name = "256";
1626 #if HAVE_MP3LAME >= 393
1627 if (strcmp(preset_name, "medium") == 0) {
1629 if (fast > 0)
1630 lame_set_preset(gfp, MEDIUM_FAST);
1631 else
1632 lame_set_preset(gfp, MEDIUM);
1634 return 0;
1636 #endif
1638 if (strcmp(preset_name, "standard") == 0) {
1640 if (fast > 0)
1641 lame_set_preset(gfp, STANDARD_FAST);
1642 else
1643 lame_set_preset(gfp, STANDARD);
1645 return 0;
1648 else if (strcmp(preset_name, "extreme") == 0){
1650 if (fast > 0)
1651 lame_set_preset(gfp, EXTREME_FAST);
1652 else
1653 lame_set_preset(gfp, EXTREME);
1655 return 0;
1658 else if (((strcmp(preset_name, "insane") == 0) ||
1659 (strcmp(preset_name, "320" ) == 0)) && (fast < 1)) {
1661 lame_set_preset(gfp, INSANE);
1663 return 0;
1666 // Generic ABR Preset
1667 if (((atoi(preset_name)) > 0) && (fast < 1)) {
1668 if ((atoi(preset_name)) >= 8 && (atoi(preset_name)) <= 320){
1669 lame_set_preset(gfp, atoi(preset_name));
1671 if (cbr == 1 )
1672 lame_set_VBR(gfp, vbr_off);
1674 if (mono == 1 ) {
1675 lame_set_mode(gfp, MONO);
1678 return 0;
1681 else {
1682 fprintf(stderr, "LAME version %s (%s)\n\n", get_lame_version(), get_lame_url());
1683 fprintf(stderr,"Error: The bitrate specified is out of the valid range for this preset\n"
1684 "\n"
1685 "When using this mode you must enter a value between \"8\" and \"320\"\n"
1686 "\n"
1687 "For further information try: \"-lameopts preset=help\"\n"
1689 return -1;
1695 fprintf(stderr, "LAME version %s (%s)\n\n", get_lame_version(), get_lame_url());
1696 fprintf(stderr,"Error: You did not enter a valid profile and/or options with preset\n"
1697 "\n"
1698 "Available profiles are:\n"
1699 "\n"
1700 " <fast> standard\n"
1701 " <fast> extreme\n"
1702 " insane\n"
1703 " <cbr> (ABR Mode) - The ABR Mode is implied. To use it,\n"
1704 " simply specify a bitrate. For example:\n"
1705 " \"preset=185\" activates this\n"
1706 " preset and uses 185 as an average kbps.\n"
1707 "\n"
1708 " Some examples:\n"
1709 "\n"
1710 " \"-lameopts fast:preset=standard \"\n"
1711 " or \"-lameopts cbr:preset=192 \"\n"
1712 " or \"-lameopts preset=172 \"\n"
1713 " or \"-lameopts preset=extreme \"\n"
1714 "\n"
1715 "For further information try: \"-lameopts preset=help\"\n"
1717 mencoder_exit(1, "error parsing cmdline");
1719 #endif
1721 #if HAVE_MP3LAME >= 392
1722 /* lame_presets_longinfo_dm
1723 taken out of presets_longinfo_dm in lame-3.93.1/frontend/parse.c and modified */
1724 static void lame_presets_longinfo_dm ( FILE* msgfp )
1726 fprintf ( msgfp,
1727 "\n"
1728 "The preset switches are designed to provide the highest possible quality.\n"
1729 "\n"
1730 "They have for the most part been subject to and tuned via rigorous double blind\n"
1731 "listening tests to verify and achieve this objective.\n"
1732 "\n"
1733 "These are continually updated to coincide with the latest developments that\n"
1734 "occur and as a result should provide you with nearly the best quality\n"
1735 "currently possible from LAME.\n"
1736 "\n"
1737 "To activate these presets:\n"
1738 "\n"
1739 " For VBR modes (generally highest quality):\n"
1740 "\n"
1741 " \"preset=standard\" This preset should generally be transparent\n"
1742 " to most people on most music and is already\n"
1743 " quite high in quality.\n"
1744 "\n"
1745 " \"preset=extreme\" If you have extremely good hearing and similar\n"
1746 " equipment, this preset will generally provide\n"
1747 " slightly higher quality than the \"standard\"\n"
1748 " mode.\n"
1749 "\n"
1750 " For CBR 320kbps (highest quality possible from the preset switches):\n"
1751 "\n"
1752 " \"preset=insane\" This preset will usually be overkill for most\n"
1753 " people and most situations, but if you must\n"
1754 " have the absolute highest quality with no\n"
1755 " regard to filesize, this is the way to go.\n"
1756 "\n"
1757 " For ABR modes (high quality per given bitrate but not as high as VBR):\n"
1758 "\n"
1759 " \"preset=<kbps>\" Using this preset will usually give you good\n"
1760 " quality at a specified bitrate. Depending on the\n"
1761 " bitrate entered, this preset will determine the\n"
1762 " optimal settings for that particular situation.\n"
1763 " While this approach works, it is not nearly as\n"
1764 " flexible as VBR, and usually will not attain the\n"
1765 " same level of quality as VBR at higher bitrates.\n"
1766 "\n"
1767 "The following options are also available for the corresponding profiles:\n"
1768 "\n"
1769 " <fast> standard\n"
1770 " <fast> extreme\n"
1771 " insane\n"
1772 " <cbr> (ABR Mode) - The ABR Mode is implied. To use it,\n"
1773 " simply specify a bitrate. For example:\n"
1774 " \"preset=185\" activates this\n"
1775 " preset and uses 185 as an average kbps.\n"
1776 "\n"
1777 " \"fast\" - Enables the new fast VBR for a particular profile. The\n"
1778 " disadvantage to the speed switch is that often times the\n"
1779 " bitrate will be slightly higher than with the normal mode\n"
1780 " and quality may be slightly lower also.\n"
1781 " Warning: with the current version fast presets might result in too\n"
1782 " high bitrate compared to regular presets.\n"
1783 "\n"
1784 " \"cbr\" - If you use the ABR mode (read above) with a significant\n"
1785 " bitrate such as 80, 96, 112, 128, 160, 192, 224, 256, 320,\n"
1786 " you can use the \"cbr\" option to force CBR mode encoding\n"
1787 " instead of the standard abr mode. ABR does provide higher\n"
1788 " quality but CBR may be useful in situations such as when\n"
1789 " streaming an mp3 over the internet may be important.\n"
1790 "\n"
1791 " For example:\n"
1792 "\n"
1793 " \"-lameopts fast:preset=standard \"\n"
1794 " or \"-lameopts cbr:preset=192 \"\n"
1795 " or \"-lameopts preset=172 \"\n"
1796 " or \"-lameopts preset=extreme \"\n"
1797 "\n"
1798 "\n"
1799 "A few aliases are available for ABR mode:\n"
1800 "phone => 16kbps/mono phon+/lw/mw-eu/sw => 24kbps/mono\n"
1801 "mw-us => 40kbps/mono voice => 56kbps/mono\n"
1802 "fm/radio/tape => 112kbps hifi => 160kbps\n"
1803 "cd => 192kbps studio => 256kbps");
1804 mencoder_exit(0, NULL);
1806 #endif
1808 #ifdef USE_LIBAVCODEC
1809 static uint32_t lavc_find_atag(char *codec)
1811 if(codec == NULL)
1812 return 0;
1814 if(! strcasecmp(codec, "mp2"))
1815 return 0x50;
1817 if(! strcasecmp(codec, "mp3"))
1818 return 0x55;
1820 if(! strcasecmp(codec, "ac3"))
1821 return 0x2000;
1823 if(! strcasecmp(codec, "adpcm_ima_wav"))
1824 return 0x11;
1826 return 0;
1828 #endif