-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmencoder.c
1866 lines (1600 loc) · 56.5 KB
/
mencoder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define VCODEC_COPY 0
#define VCODEC_FRAMENO 1
// real codecs:
#define VCODEC_DIVX4 2
#define VCODEC_LIBAVCODEC 4
#define VCODEC_VFW 7
#define VCODEC_LIBDV 8
#define VCODEC_XVID 9
#define VCODEC_QTVIDEO 10
#define VCODEC_NUV 11
#define VCODEC_RAW 12
#define VCODEC_X264 13
#define ACODEC_COPY 0
#define ACODEC_PCM 1
#define ACODEC_VBRMP3 2
#define ACODEC_NULL 3
#define ACODEC_LAVC 4
#define ACODEC_TOOLAME 5
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "config.h"
#ifdef __MINGW32__
#define SIGQUIT 3
#endif
#ifdef WIN32
#include <windows.h>
#endif
#include <sys/time.h>
#include "version.h"
#include "mp_msg.h"
#include "help_mp.h"
#include "cpudetect.h"
#include "codec-cfg.h"
#include "m_option.h"
#include "m_config.h"
#include "parser-mecmd.h"
#include "libmpdemux/stream.h"
#include "libmpdemux/demuxer.h"
#include "libmpdemux/stheader.h"
#include "libmpdemux/mp3_hdr.h"
#include "libmpdemux/muxer.h"
#include "libvo/video_out.h"
#include "libao2/afmt.h"
#include "libmpcodecs/mp_image.h"
#include "libmpcodecs/dec_audio.h"
#include "libmpcodecs/dec_video.h"
#include "libmpcodecs/vf.h"
// for MPEGLAYER3WAVEFORMAT:
#include "libmpdemux/ms_hdr.h"
#ifdef HAVE_MP3LAME
#undef CDECL
#include <lame/lame.h>
#endif
#include <inttypes.h>
#include "libvo/fastmemcpy.h"
#include "osdep/timer.h"
#ifdef USE_LIBAVCODEC
// for lavc audio encoding
#ifdef USE_LIBAVCODEC_SO
#include <ffmpeg/avcodec.h>
#else
#include "libavcodec/avcodec.h"
#endif
static AVCodec *lavc_acodec;
static AVCodecContext *lavc_actx = NULL;
extern char *lavc_param_acodec;
extern int lavc_param_abitrate;
extern int lavc_param_atag;
// tmp buffer for lavc audio encoding (to free!!!!!)
static void *lavc_abuf = NULL;
extern int avcodec_inited;
static uint32_t lavc_find_atag(char *codec);
#endif
#ifdef HAVE_TOOLAME
#include "libmpcodecs/ae_toolame.h"
static mpae_toolame_ctx *mpae_toolame;
#endif
int vo_doublebuffering=0;
int vo_directrendering=0;
int vo_config_count=0;
int forced_subs_only=0;
//--------------------------
// cache2:
int stream_cache_size=-1;
#ifdef USE_STREAM_CACHE
extern int cache_fill_status;
float stream_cache_min_percent=20.0;
float stream_cache_prefill_percent=5.0;
#else
#define cache_fill_status 0
#endif
int audio_id=-1;
int video_id=-1;
int dvdsub_id=-1;
int vobsub_id=-1;
char* audio_lang=NULL;
char* dvdsub_lang=NULL;
static char* spudec_ifo=NULL;
static char** audio_codec_list=NULL; // override audio codec
static char** video_codec_list=NULL; // override video codec
static char** audio_fm_list=NULL; // override audio codec family
static char** video_fm_list=NULL; // override video codec family
static int out_audio_codec=-1;
static int out_video_codec=-1;
int out_file_format=MUXER_TYPE_AVI; // default to AVI
// audio stream skip/resync functions requires only for seeking.
// (they should be implemented in the audio codec layer)
//void skip_audio_frame(sh_audio_t *sh_audio){}
//void resync_audio_stream(sh_audio_t *sh_audio){}
int verbose=0; // must be global!
int identify=0;
int quiet=0;
double video_time_usage=0;
double vout_time_usage=0;
double max_video_time_usage=0;
double max_vout_time_usage=0;
double cur_video_time_usage=0;
double cur_vout_time_usage=0;
int benchmark=0;
// A-V sync:
int delay_corrected=1;
static float default_max_pts_correction=-1;//0.01f;
static float max_pts_correction=0;//default_max_pts_correction;
static float c_total=0;
static float audio_preload=0.5;
static float audio_delay=0.0;
static int audio_density=2;
float force_fps=0;
static float force_ofps=0; // set to 24 for inverse telecine
static int skip_limit=-1;
static int force_srate=0;
static int audio_output_format=0;
char *vobsub_out=NULL;
unsigned int vobsub_out_index=0;
char *vobsub_out_id=NULL;
char* out_filename="test.avi";
char *force_fourcc=NULL;
char* passtmpfile="divx2pass.log";
static int play_n_frames=-1;
static int play_n_frames_mf=-1;
#include "libvo/font_load.h"
#include "libvo/sub.h"
// sub:
char *font_name=NULL;
#ifdef HAVE_FONTCONFIG
extern int font_fontconfig;
#endif
float font_factor=0.75;
char **sub_name=NULL;
float sub_delay=0;
float sub_fps=0;
int sub_auto = 0;
int subcc_enabled=0;
int suboverlap_enabled = 1;
#ifdef USE_SUB
static sub_data* subdata=NULL;
float sub_last_pts = -303;
#endif
int auto_expand=1;
int encode_duplicates=1;
// infos are empty by default
char *info_name=NULL;
char *info_artist=NULL;
char *info_genre=NULL;
char *info_subject=NULL;
char *info_copyright=NULL;
char *info_sourceform=NULL;
char *info_comment=NULL;
//char *out_audio_codec=NULL; // override audio codec
//char *out_video_codec=NULL; // override video codec
//#include "libmpeg2/mpeg2.h"
//#include "libmpeg2/mpeg2_internal.h"
#ifdef HAVE_MP3LAME
int lame_param_quality=0; // best
int lame_param_algqual=5; // same as old default
int lame_param_vbr=vbr_default;
int lame_param_mode=-1; // unset
int lame_param_padding=-1; // unset
int lame_param_br=-1; // unset
int lame_param_ratio=-1; // unset
float lame_param_scale=-1; // unset
int lame_param_lowpassfreq = 0; //auto
int lame_param_highpassfreq = 0; //auto
int lame_param_free_format = 0; //disabled
int lame_param_br_min = 0; //not specified
int lame_param_br_max = 0; //not specified
#if HAVE_MP3LAME >= 392
int lame_param_fast=0; // unset
static char* lame_param_preset=NULL; // unset
static int lame_presets_set( lame_t gfp, int fast, int cbr, const char* preset_name );
static void lame_presets_longinfo_dm ( FILE* msgfp );
#endif
#endif
//static int vo_w=0, vo_h=0;
//-------------------------- config stuff:
m_config_t* mconfig;
extern int m_config_parse_config_file(m_config_t* config, char *conffile);
static int cfg_inc_verbose(m_option_t *conf){ ++verbose; return 0;}
static int cfg_include(m_option_t *conf, char *filename){
return m_config_parse_config_file(mconfig, filename);
}
static char *seek_to_sec=NULL;
static off_t seek_to_byte=0;
static int parse_end_at(m_option_t *conf, const char* param);
//static uint8_t* flip_upside_down(uint8_t* dst, const uint8_t* src, int width, int height);
#include "get_path.c"
#include "cfg-mplayer-def.h"
#include "cfg-mencoder.h"
#ifdef USE_DVDREAD
#include "spudec.h"
#endif
#include "vobsub.h"
/* FIXME */
static void mencoder_exit(int level, char *how)
{
if (how)
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_ExitingHow, mp_gettext(how));
else
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_Exiting);
exit(level);
}
void parse_cfgfiles( m_config_t* conf )
{
char *conffile;
if ((conffile = get_path("mencoder")) == NULL) {
mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
} else {
if (m_config_parse_config_file(conf, conffile) < 0)
mencoder_exit(1,MSGTR_ConfigfileError);
free(conffile);
}
}
//---------------------------------------------------------------------------
static int dec_audio(sh_audio_t *sh_audio,unsigned char* buffer,int total){
int size=0;
int at_eof=0;
while(size<total && !at_eof){
int len=total-size;
if(len>MAX_OUTBURST) len=MAX_OUTBURST;
if(len>sh_audio->a_out_buffer_size) len=sh_audio->a_out_buffer_size;
if(len>sh_audio->a_out_buffer_len){
int ret=decode_audio(sh_audio,
&sh_audio->a_out_buffer[sh_audio->a_out_buffer_len],
len-sh_audio->a_out_buffer_len,
sh_audio->a_out_buffer_size-sh_audio->a_out_buffer_len);
if(ret>0) sh_audio->a_out_buffer_len+=ret; else at_eof=1;
}
if(len>sh_audio->a_out_buffer_len) len=sh_audio->a_out_buffer_len;
memcpy(buffer+size,sh_audio->a_out_buffer,len);
sh_audio->a_out_buffer_len-=len; size+=len;
if(sh_audio->a_out_buffer_len>0)
memcpy(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[len],sh_audio->a_out_buffer_len);
}
return size;
}
//---------------------------------------------------------------------------
static int at_eof=0;
static int interrupted=0;
enum end_at_type_t {END_AT_NONE, END_AT_TIME, END_AT_SIZE};
static enum end_at_type_t end_at_type = END_AT_NONE;
static double end_at;
static void exit_sighandler(int x){
at_eof=1;
interrupted=2; /* 1 means error */
}
static muxer_t* muxer=NULL;
static FILE* muxer_f=NULL;
extern void print_wave_header(WAVEFORMATEX *h);
int main(int argc,char* argv[]){
stream_t* stream=NULL;
demuxer_t* demuxer=NULL;
stream_t* stream2=NULL;
demuxer_t* demuxer2=NULL;
demux_stream_t *d_audio=NULL;
demux_stream_t *d_video=NULL;
demux_stream_t *d_dvdsub=NULL;
sh_audio_t *sh_audio=NULL;
sh_video_t *sh_video=NULL;
int file_format=DEMUXER_TYPE_UNKNOWN;
int i=DEMUXER_TYPE_UNKNOWN;
void *vobsub_writer=NULL;
uint32_t ptimer_start;
uint32_t audiorate=0;
uint32_t videorate=0;
uint32_t audiosamples=1;
uint32_t videosamples=1;
uint32_t skippedframes=0;
uint32_t duplicatedframes=0;
uint32_t badframes=0;
muxer_stream_t* mux_a=NULL;
muxer_stream_t* mux_v=NULL;
off_t muxer_f_size=0;
#ifdef HAVE_MP3LAME
lame_global_flags *lame;
#endif
double v_pts_corr=0;
double v_timer_corr=0;
m_entry_t* filelist = NULL;
char* filename=NULL;
char* frameno_filename="frameno.avi";
int decoded_frameno=0;
int next_frameno=-1;
unsigned int timer_start;
mp_msg_init();
mp_msg_set_level(MSGL_STATUS);
mp_msg(MSGT_CPLAYER,MSGL_INFO, "MEncoder " VERSION " (C) 2000-2004 MPlayer Team\n");
/* Test for cpu capabilities (and corresponding OS support) for optimizing */
GetCpuCaps(&gCpuCaps);
#ifdef ARCH_X86
mp_msg(MSGT_CPLAYER,MSGL_INFO,"CPUflags: Type: %d MMX: %d MMX2: %d 3DNow: %d 3DNow2: %d SSE: %d SSE2: %d\n",
gCpuCaps.cpuType,gCpuCaps.hasMMX,gCpuCaps.hasMMX2,
gCpuCaps.has3DNow, gCpuCaps.has3DNowExt,
gCpuCaps.hasSSE, gCpuCaps.hasSSE2);
#ifdef RUNTIME_CPUDETECT
mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithRuntimeDetection);
#else
mp_msg(MSGT_CPLAYER,MSGL_INFO, MSGTR_CompiledWithCPUExtensions);
#ifdef HAVE_MMX
mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX");
#endif
#ifdef HAVE_MMX2
mp_msg(MSGT_CPLAYER,MSGL_INFO," MMX2");
#endif
#ifdef HAVE_3DNOW
mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNow");
#endif
#ifdef HAVE_3DNOWEX
mp_msg(MSGT_CPLAYER,MSGL_INFO," 3DNowEx");
#endif
#ifdef HAVE_SSE
mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE");
#endif
#ifdef HAVE_SSE2
mp_msg(MSGT_CPLAYER,MSGL_INFO," SSE2");
#endif
mp_msg(MSGT_CPLAYER,MSGL_INFO,"\n\n");
#endif
#endif
InitTimer();
// check codec.conf
if(!codecs_file || !parse_codec_cfg(codecs_file)){
if(!parse_codec_cfg(get_path("codecs.conf"))){
if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
if(!parse_codec_cfg(NULL)){
mp_msg(MSGT_MENCODER,MSGL_HINT,MSGTR_CopyCodecsConf);
mencoder_exit(1,NULL);
}
mp_msg(MSGT_MENCODER,MSGL_V,MSGTR_BuiltinCodecsConf);
}
}
}
// FIXME: get rid of -dvd and other tricky options
stream2=open_stream(frameno_filename,0,&i);
if(stream2){
demuxer2=demux_open(stream2,DEMUXER_TYPE_AVI,-1,-1,-2,NULL);
if(demuxer2) mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_UsingPass3ControllFile, frameno_filename);
else mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized);
}
mconfig = m_config_new();
m_config_register_options(mconfig,mencoder_opts);
parse_cfgfiles(mconfig);
filelist = m_config_parse_me_command_line(mconfig, argc, argv);
if(!filelist) mencoder_exit(1, MSGTR_ErrorParsingCommandLine);
m_entry_set_options(mconfig,&filelist[0]);
filename = filelist[0].name;
if(!filename){
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_MissingFilename);
mencoder_exit(1,NULL);
}
mp_msg_set_level(verbose+MSGL_STATUS);
// check font
#ifdef USE_OSD
#ifdef HAVE_FREETYPE
init_freetype();
#endif
#ifdef HAVE_FONTCONFIG
if(!font_fontconfig)
{
#endif
if(font_name){
vo_font=read_font_desc(font_name,font_factor,verbose>1);
if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,font_name);
} else {
// try default:
vo_font=read_font_desc(get_path("font/font.desc"),font_factor,verbose>1);
if(!vo_font)
vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
}
#ifdef HAVE_FONTCONFIG
}
#endif
#endif
vo_init_osd();
stream=open_stream(filename,0,&file_format);
if(!stream){
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_CannotOpenFile_Device);
mencoder_exit(1,NULL);
}
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_OpenedStream, file_format, (int)(stream->start_pos), (int)(stream->end_pos));
#ifdef USE_DVDREAD
if(stream->type==STREAMTYPE_DVD){
if(audio_lang && audio_id==-1) audio_id=dvd_aid_from_lang(stream,audio_lang);
if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=dvd_sid_from_lang(stream,dvdsub_lang);
}
#endif
stream->start_pos+=seek_to_byte;
if(stream_cache_size>0) stream_enable_cache(stream,stream_cache_size*1024,0,0);
if(demuxer2) audio_id=-2; /* do NOT read audio packets... */
//demuxer=demux_open(stream,file_format,video_id,audio_id,dvdsub_id);
demuxer=demux_open(stream,file_format,audio_id,video_id,dvdsub_id,filename);
if(!demuxer){
mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized);
mp_msg(MSGT_DEMUXER, MSGL_ERR, MSGTR_CannotOpenDemuxer); //correct target/level? FIXME?
mencoder_exit(1,NULL);
}
d_audio=demuxer2 ? demuxer2->audio : demuxer->audio;
d_video=demuxer->video;
d_dvdsub=demuxer->sub;
sh_audio=d_audio->sh;
sh_video=d_video->sh;
if(!sh_video)
{
mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_VideoStreamRequired);
mencoder_exit(1,NULL);
}
if(!video_read_properties(sh_video)){
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_CannotReadVideoProperties);
mencoder_exit(1,NULL);
}
mp_msg(MSGT_MENCODER,MSGL_INFO,"[V] filefmt:%d fourcc:0x%X size:%dx%d fps:%5.2f ftime:=%6.4f\n",
demuxer->file_format,sh_video->format, sh_video->disp_w,sh_video->disp_h,
sh_video->fps,sh_video->frametime
);
if(force_fps){
sh_video->fps=force_fps;
sh_video->frametime=1.0f/sh_video->fps;
mp_msg(MSGT_MENCODER,MSGL_INFO,MSGTR_ForcingInputFPS, sh_video->fps);
}
if(sh_audio && out_file_format==MUXER_TYPE_RAWVIDEO){
mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_RawvideoDoesNotSupportAudio);
sh_audio=NULL;
}
if(sh_audio && out_audio_codec<0){
if(audio_id==-2)
mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_DemuxerDoesntSupportNosound);
mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoAudioEncoderSelected);
mencoder_exit(1,NULL);
}
if(sh_video && out_video_codec<0){
mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_NoVideoEncoderSelected);
mencoder_exit(1,NULL);
}
if(sh_audio && (out_audio_codec || seek_to_sec || !sh_audio->wf)){
// Go through the codec.conf and find the best codec...
mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
if(!init_best_audio_codec(sh_audio,audio_codec_list,audio_fm_list)){
sh_audio=d_audio->sh=NULL; // failed to init :(
}
mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
}
// set up video encoder:
if (vobsub_out) {
unsigned int palette[16], width, height;
unsigned char tmp[3] = { 0, 0, 0 };
if (spudec_ifo && vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, dvdsub_id, tmp) >= 0)
vobsub_writer = vobsub_out_open(vobsub_out, palette, sh_video->disp_w, sh_video->disp_h,
vobsub_out_id?vobsub_out_id:(char *)tmp, vobsub_out_index);
#ifdef USE_DVDREAD
if (vobsub_writer == NULL) {
char tmp[3];
if (vobsub_out_id == NULL && stream->type == STREAMTYPE_DVD) {
int i;
dvd_priv_t *dvd = (dvd_priv_t*)stream->priv;
for (i = 0; i < dvd->nr_of_subtitles; ++i)
if (dvd->subtitles[i].id == dvdsub_id) {
tmp[0] = (dvd->subtitles[i].language >> 8) & 0xff;
tmp[1] = dvd->subtitles[i].language & 0xff;
tmp[2] = 0;
vobsub_out_id = tmp;
break;
}
}
vobsub_writer=vobsub_out_open(vobsub_out, stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
sh_video->disp_w, sh_video->disp_h, vobsub_out_id, vobsub_out_index);
}
#endif
}
else {
if (spudec_ifo) {
unsigned int palette[16], width, height;
if (vobsub_parse_ifo(NULL,spudec_ifo, palette, &width, &height, 1, -1, NULL) >= 0)
vo_spudec=spudec_new_scaled(palette, sh_video->disp_w, sh_video->disp_h);
}
#ifdef USE_DVDREAD
if (vo_spudec==NULL) {
vo_spudec=spudec_new_scaled(stream->type==STREAMTYPE_DVD?((dvd_priv_t *)(stream->priv))->cur_pgc->palette:NULL,
sh_video->disp_w, sh_video->disp_h);
}
#endif
}
#ifdef USE_SUB
// after reading video params we should load subtitles because
// we know fps so now we can adjust subtitles time to ~6 seconds AST
// check .sub
// current_module="read_subtitles_file";
if(sub_name && sub_name[0]){
subdata=sub_read_file(sub_name[0], sh_video->fps);
if(!subdata) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadSub,sub_name[0]);
} else
if(sub_auto) { // auto load sub file ...
subdata=sub_read_file( filename ? sub_filenames( get_path("sub/"), filename )[0]
: "default.sub", sh_video->fps );
}
#endif
// Apply current settings for forced subs
spudec_set_forced_subs_only(vo_spudec,forced_subs_only);
// set up output file:
muxer_f=fopen(out_filename,"wb");
if(!muxer_f) {
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_CannotOpenOutputFile, out_filename);
mencoder_exit(1,NULL);
}
muxer=muxer_new_muxer(out_file_format,muxer_f);
// ============= VIDEO ===============
mux_v=muxer_new_stream(muxer,MUXER_TYPE_VIDEO);
mux_v->buffer_size=0x200000; // 2MB
mux_v->buffer=malloc(mux_v->buffer_size);
mux_v->source=sh_video;
mux_v->h.dwSampleSize=0; // VBR
#ifdef USE_LIBAVCODEC
{
AVRational q= av_d2q(force_ofps?force_ofps:sh_video->fps, 30000);
mux_v->h.dwScale= q.den;
mux_v->h.dwRate = q.num;
}
#else
mux_v->h.dwScale=10000;
mux_v->h.dwRate=mux_v->h.dwScale*(force_ofps?force_ofps:sh_video->fps);
#endif
mux_v->codec=out_video_codec;
mux_v->bih=NULL;
sh_video->codec=NULL;
sh_video->video_out=NULL;
sh_video->vfilter=NULL; // fixme!
switch(mux_v->codec){
case VCODEC_COPY:
if (sh_video->bih)
mux_v->bih=sh_video->bih;
else
{
mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
mux_v->bih->biWidth=sh_video->disp_w;
mux_v->bih->biHeight=sh_video->disp_h;
mux_v->bih->biCompression=sh_video->format;
mux_v->bih->biPlanes=1;
mux_v->bih->biBitCount=24; // FIXME!!!
mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
}
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_VCodecFramecopy,
mux_v->bih->biWidth, mux_v->bih->biHeight,
mux_v->bih->biBitCount, mux_v->bih->biCompression);
break;
case VCODEC_FRAMENO:
mux_v->bih=calloc(1,sizeof(BITMAPINFOHEADER));
mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
mux_v->bih->biWidth=sh_video->disp_w;
mux_v->bih->biHeight=sh_video->disp_h;
mux_v->bih->biPlanes=1;
mux_v->bih->biBitCount=24;
mux_v->bih->biCompression=mmioFOURCC('F','r','N','o');
mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
break;
default:
switch(mux_v->codec){
case VCODEC_DIVX4:
sh_video->vfilter=vf_open_encoder(NULL,"divx4",(char *)mux_v); break;
case VCODEC_LIBAVCODEC:
sh_video->vfilter=vf_open_encoder(NULL,"lavc",(char *)mux_v); break;
case VCODEC_RAW:
sh_video->vfilter=vf_open_encoder(NULL,"raw",(char *)mux_v); break;
case VCODEC_VFW:
sh_video->vfilter=vf_open_encoder(NULL,"vfw",(char *)mux_v); break;
case VCODEC_LIBDV:
sh_video->vfilter=vf_open_encoder(NULL,"libdv",(char *)mux_v); break;
case VCODEC_XVID:
sh_video->vfilter=vf_open_encoder(NULL,"xvid",(char *)mux_v); break;
case VCODEC_QTVIDEO:
sh_video->vfilter=vf_open_encoder(NULL,"qtvideo",(char *)mux_v); break;
case VCODEC_NUV:
sh_video->vfilter=vf_open_encoder(NULL,"nuv",(char *)mux_v); break;
case VCODEC_X264:
sh_video->vfilter=vf_open_encoder(NULL,"x264",(char *)mux_v); break;
}
if(!mux_v->bih || !sh_video->vfilter){
mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_EncoderOpenFailed);
mencoder_exit(1,NULL);
}
// append 'expand' filter, it fixes stride problems and renders osd:
if (auto_expand) {
char* vf_args[] = { "osd", "1", NULL };
sh_video->vfilter=vf_open_filter(sh_video->vfilter,"expand",vf_args);
}
sh_video->vfilter=append_filters(sh_video->vfilter);
mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
init_best_video_codec(sh_video,video_codec_list,video_fm_list);
mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
if(!sh_video->inited) mencoder_exit(1,NULL);
}
/* force output fourcc to .. */
if ((force_fourcc != NULL) && (strlen(force_fourcc) >= 4))
{
mux_v->bih->biCompression = mmioFOURCC(force_fourcc[0], force_fourcc[1],
force_fourcc[2], force_fourcc[3]);
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_ForcingOutputFourcc,
mux_v->bih->biCompression, (char *)&mux_v->bih->biCompression);
}
//if(demuxer->file_format!=DEMUXER_TYPE_AVI) pts_from_bps=0; // it must be 0 for mpeg/asf!
// ============= AUDIO ===============
if(sh_audio){
mux_a=muxer_new_stream(muxer,MUXER_TYPE_AUDIO);
mux_a->buffer_size=0x100000; //16384;
mux_a->buffer=malloc(mux_a->buffer_size);
if (!mux_a->buffer)
mencoder_exit(1,MSGTR_MemAllocFailed);
mux_a->source=sh_audio;
mux_a->codec=out_audio_codec;
switch(mux_a->codec){
case ACODEC_COPY:
if (sh_audio->wf){
mux_a->wf=sh_audio->wf;
if(!sh_audio->i_bps) sh_audio->i_bps=mux_a->wf->nAvgBytesPerSec;
} else {
mux_a->wf = malloc(sizeof(WAVEFORMATEX));
mux_a->wf->nBlockAlign = 1; //mux_a->h.dwSampleSize;
mux_a->wf->wFormatTag = sh_audio->format;
mux_a->wf->nChannels = sh_audio->channels;
mux_a->wf->nSamplesPerSec = sh_audio->samplerate;
mux_a->wf->nAvgBytesPerSec=sh_audio->i_bps; //mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
mux_a->wf->wBitsPerSample = 16; // FIXME
mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
}
if(sh_audio->audio.dwScale){
mux_a->h.dwSampleSize=sh_audio->audio.dwSampleSize;
mux_a->h.dwScale=sh_audio->audio.dwScale;
mux_a->h.dwRate=sh_audio->audio.dwRate;
// mux_a->h.dwStart=sh_audio->audio.dwStart;
} else {
mux_a->h.dwSampleSize=mux_a->wf->nBlockAlign;
mux_a->h.dwScale=mux_a->h.dwSampleSize;
mux_a->h.dwRate=mux_a->wf->nAvgBytesPerSec;
}
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_ACodecFramecopy,
mux_a->wf->wFormatTag, mux_a->wf->nChannels, mux_a->wf->nSamplesPerSec,
mux_a->wf->wBitsPerSample, mux_a->wf->nAvgBytesPerSec, mux_a->h.dwSampleSize);
break;
case ACODEC_PCM:
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_CBRPCMAudioSelected);
mux_a->h.dwScale=1;
mux_a->h.dwRate=force_srate?force_srate:sh_audio->samplerate;
mux_a->wf=malloc(sizeof(WAVEFORMATEX));
mux_a->wf->wFormatTag=0x1; // PCM
mux_a->wf->nChannels=audio_output_channels?audio_output_channels:sh_audio->channels;
mux_a->h.dwSampleSize=2*mux_a->wf->nChannels;
mux_a->wf->nBlockAlign=mux_a->h.dwSampleSize;
mux_a->wf->nSamplesPerSec=mux_a->h.dwRate;
mux_a->wf->nAvgBytesPerSec=mux_a->h.dwSampleSize*mux_a->wf->nSamplesPerSec;
mux_a->wf->wBitsPerSample=16;
mux_a->wf->cbSize=0; // FIXME for l3codeca.acm
// setup filter:
if(!init_audio_filters(sh_audio,
sh_audio->samplerate,
sh_audio->channels, sh_audio->sample_format, sh_audio->samplesize,
mux_a->wf->nSamplesPerSec, mux_a->wf->nChannels,
(mux_a->wf->wBitsPerSample==8)? AFMT_U8:AFMT_S16_LE,
mux_a->wf->wBitsPerSample/8,
16384, mux_a->wf->nAvgBytesPerSec)){
mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
}
break;
#ifdef HAVE_MP3LAME
case ACODEC_VBRMP3:
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_MP3AudioSelected);
mux_a->h.dwSampleSize=0; // VBR
mux_a->h.dwRate=force_srate?force_srate:sh_audio->samplerate;
mux_a->h.dwScale=(mux_a->h.dwRate<32000)?576:1152; // samples/frame
if(sizeof(MPEGLAYER3WAVEFORMAT)!=30) mp_msg(MSGT_MENCODER,MSGL_WARN,MSGTR_MP3WaveFormatSizeNot30,sizeof(MPEGLAYER3WAVEFORMAT));
mux_a->wf=malloc(sizeof(MPEGLAYER3WAVEFORMAT)); // should be 30
mux_a->wf->wFormatTag=0x55; // MP3
mux_a->wf->nChannels= (lame_param_mode<0) ? sh_audio->channels :
((lame_param_mode==3) ? 1 : 2);
mux_a->wf->nSamplesPerSec=mux_a->h.dwRate;
mux_a->wf->nAvgBytesPerSec=192000/8; // FIXME!
mux_a->wf->nBlockAlign=(mux_a->h.dwRate<32000)?576:1152; // required for l3codeca.acm + WMP 6.4
mux_a->wf->wBitsPerSample=0; //16;
// from NaNdub: (requires for l3codeca.acm)
mux_a->wf->cbSize=12;
((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->wID=1;
((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->fdwFlags=2;
((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nBlockSize=(mux_a->h.dwRate<32000)?576:1152; // ???
((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nFramesPerBlock=1;
((MPEGLAYER3WAVEFORMAT*)(mux_a->wf))->nCodecDelay=0;
// setup filter:
if(!init_audio_filters(sh_audio,
sh_audio->samplerate,
sh_audio->channels, sh_audio->sample_format, sh_audio->samplesize,
mux_a->wf->nSamplesPerSec, mux_a->wf->nChannels,
#ifdef WORDS_BIGENDIAN
AFMT_S16_BE, 2,
#else
AFMT_S16_LE, 2,
#endif
4608, mux_a->h.dwRate*mux_a->wf->nChannels*2)){
mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_NoMatchingFilter);
}
break;
#endif
#ifdef USE_LIBAVCODEC
case ACODEC_LAVC:
if(!lavc_param_acodec)
{
mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_NoLavcAudioCodecName);
exit(1);
}
if(!avcodec_inited){
avcodec_init();
avcodec_register_all();
avcodec_inited=1;
}
lavc_acodec = avcodec_find_encoder_by_name(lavc_param_acodec);
if (!lavc_acodec)
{
mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_LavcAudioCodecNotFound, lavc_param_acodec);
exit(1);
}
lavc_actx = avcodec_alloc_context();
if(lavc_actx == NULL)
{
mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntAllocateLavcContext);
exit(1);
}
if(lavc_param_atag == 0)
lavc_param_atag = lavc_find_atag(lavc_param_acodec);
// put sample parameters
lavc_actx->channels = audio_output_channels ? audio_output_channels : sh_audio->channels;
lavc_actx->sample_rate = force_srate ? force_srate : sh_audio->samplerate;
lavc_actx->bit_rate = lavc_param_abitrate * 1000;
/*
* Special case for imaadpcm.
* The bitrate is only dependant on samplerate.
* We have to known frame_size and block_align in advance,
* so I just copied the code from libavcodec/adpcm.c
*
* However, ms imaadpcm uses a block_align of 2048,
* lavc defaults to 1024
*/
if(lavc_param_atag == 0x11) {
int blkalign = 2048;
int framesize = (blkalign - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1;
lavc_actx->bit_rate = lavc_actx->sample_rate*8*blkalign/framesize;
}
if(avcodec_open(lavc_actx, lavc_acodec) < 0)
{
mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CouldntOpenCodec, lavc_param_acodec, lavc_param_abitrate);
exit(1);
}
if(lavc_param_atag == 0x11) {
lavc_actx->block_align = 2048;
lavc_actx->frame_size = (lavc_actx->block_align - 4 * lavc_actx->channels) * 8 / (4 * lavc_actx->channels) + 1;
}
lavc_abuf = malloc(lavc_actx->frame_size * 2 * lavc_actx->channels);
if(lavc_abuf == NULL)
{
mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_CannotAllocateBytes, lavc_actx->frame_size * 2 * lavc_actx->channels); // Converted from fprintf(stderr, ...);
exit(1);
}
mux_a->wf = malloc(sizeof(WAVEFORMATEX)+lavc_actx->extradata_size+256);
mux_a->wf->wFormatTag = lavc_param_atag;
mux_a->wf->nChannels = lavc_actx->channels;
mux_a->wf->nSamplesPerSec = lavc_actx->sample_rate;
mux_a->wf->nAvgBytesPerSec = (lavc_actx->bit_rate / 8);
mux_a->h.dwRate = mux_a->wf->nAvgBytesPerSec;
if (lavc_actx->block_align) {
mux_a->h.dwSampleSize = mux_a->h.dwScale = lavc_actx->block_align;
} else {
mux_a->h.dwScale = (mux_a->wf->nAvgBytesPerSec * lavc_actx->frame_size)/ mux_a->wf->nSamplesPerSec; /* for cbr */
if ((mux_a->wf->nAvgBytesPerSec *
lavc_actx->frame_size) % mux_a->wf->nSamplesPerSec) {
mux_a->h.dwScale = lavc_actx->frame_size;
mux_a->h.dwRate = lavc_actx->sample_rate;
mux_a->h.dwSampleSize = 0; // Blocksize not constant
} else {
mux_a->h.dwSampleSize = mux_a->h.dwScale;
}
}
mux_a->wf->nBlockAlign = mux_a->h.dwScale;
mux_a->h.dwSuggestedBufferSize = audio_preload*mux_a->wf->nAvgBytesPerSec;
mux_a->h.dwSuggestedBufferSize -= mux_a->h.dwSuggestedBufferSize % mux_a->wf->nBlockAlign;
switch (lavc_param_atag) {
case 0x11: /* imaadpcm */
mux_a->wf->wBitsPerSample = 4;
mux_a->wf->cbSize = 2;
((uint16_t*)mux_a->wf)[sizeof(WAVEFORMATEX)] =
((lavc_actx->block_align - 4 * lavc_actx->channels) / (4 * lavc_actx->channels)) * 8 + 1;
break;
case 0x55: /* mp3 */
mux_a->wf->cbSize = 12;
mux_a->wf->wBitsPerSample = 0; /* does not apply */
((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->wID = 1;
((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->fdwFlags = 2;
((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nBlockSize = mux_a->wf->nBlockAlign;
((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nFramesPerBlock = 1;
((MPEGLAYER3WAVEFORMAT *) (mux_a->wf))->nCodecDelay = 0;
break;
default:
mux_a->wf->wBitsPerSample = 0; /* Unknown */
if (lavc_actx->extradata && (lavc_actx->extradata_size > 0))
{
memcpy(mux_a->wf+sizeof(WAVEFORMATEX), lavc_actx->extradata,
lavc_actx->extradata_size);
mux_a->wf->cbSize = lavc_actx->extradata_size;
}
else
mux_a->wf->cbSize = 0;
break;
}
// Fix allocation
mux_a->wf = realloc(mux_a->wf, sizeof(WAVEFORMATEX)+mux_a->wf->cbSize);
// setup filter:
if (!init_audio_filters(
sh_audio,
sh_audio->samplerate, sh_audio->channels,
sh_audio->sample_format, sh_audio->samplesize,
mux_a->wf->nSamplesPerSec, mux_a->wf->nChannels,
AFMT_S16_NE, 2,
mux_a->h.dwSuggestedBufferSize,
mux_a->h.dwSuggestedBufferSize*2)) {
mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_NoMatchingFilter);
exit(1);
}
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);
break;
#endif
#ifdef HAVE_TOOLAME
case ACODEC_TOOLAME:
{