-
Notifications
You must be signed in to change notification settings - Fork 21
/
mencoder.c
1802 lines (1576 loc) · 58.6 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
/*
* This file is part of MPlayer.
*
* MPlayer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define VCODEC_COPY 0
#define VCODEC_FRAMENO 1
// real codecs:
#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
#define ACODEC_FAAC 6
#define ACODEC_TWOLAME 7
#ifdef __MINGW32__
#define SIGHUP 1
#define SIGQUIT 3
#define SIGPIPE 13
#endif
#include "config.h"
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#if defined(__MINGW32__) || defined(__CYGWIN__)
#include <windows.h>
#endif
#include "input/input.h"
#include "libaf/af_format.h"
#include "libao2/audio_out.h"
#include "libavcodec/avcodec.h"
#include "libmpcodecs/ae.h"
#include "libmpcodecs/dec_audio.h"
#include "libmpcodecs/dec_video.h"
#include "libmpcodecs/mp_image.h"
#include "libmpcodecs/vd.h"
#include "libmpcodecs/vf.h"
#include "libmpdemux/aviprint.h"
#include "libmpdemux/demuxer.h"
#include "libmpdemux/mp3_hdr.h"
#include "libmpdemux/ms_hdr.h"
#include "libmpdemux/muxer.h"
#include "libmpdemux/stheader.h"
#include "libvo/fastmemcpy.h"
#include "sub/font_load.h"
#include "sub/sub.h"
#include "libvo/video_out.h"
#include "osdep/timer.h"
#include "stream/stream.h"
#include "stream/stream_bd.h"
#ifdef CONFIG_DVDREAD
#include "stream/stream_dvd.h"
#endif
#include "stream/stream_dvdnav.h"
#include "sub/ass_mp.h"
#include "codec-cfg.h"
#include "edl.h"
#include "help_mp.h"
#include "m_config.h"
#include "m_option.h"
#include "mp_fifo.h"
#include "mp_msg.h"
#include "mpcommon.h"
#include "parser-cfg.h"
#include "parser-mecmd.h"
#include "path.h"
#include "sub/spudec.h"
#include "sub/vobsub.h"
#include "sub/eosd.h"
#include "mencoder.h"
int vo_doublebuffering=0;
int vo_directrendering=0;
int vo_config_count=1;
//--------------------------
// cache2:
int stream_cache_size=-1;
#ifdef CONFIG_STREAM_CACHE
float stream_cache_min_percent=20.0;
float stream_cache_seek_min_percent=50.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;
char* filename=NULL;
int channel_state = 0;
int coreavc_codec=0;
int is_mpegts_format=0;
#ifdef CONFIG_ICONV
extern char *url_cp;
extern char *sub_cps;
#endif
#ifdef CONFIG_ASS
int keep_pts=0;
#endif
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
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:
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_fix=0.0;
static float audio_delay=0.0;
static int ignore_start=0;
static int audio_density=2;
double force_fps=0;
static double force_ofps=0; // set to 24 for inverse telecine
static int skip_limit=-1;
float playback_speed=1.0;
static int force_srate=0;
static int audio_output_format=AF_FORMAT_UNKNOWN;
char *vobsub_out=NULL;
unsigned int vobsub_out_index=0;
char *vobsub_out_id=NULL;
char* out_filename=NULL;
char *force_fourcc=NULL;
int force_audiofmttag=-1;
char* passtmpfile="divx2pass.log";
static int play_n_frames=-1;
static int play_n_frames_mf=-1;
// sub:
char *font_name=NULL;
char *sub_font_name=NULL;
float font_factor=0.75;
char **sub_name=NULL;
char **sub_paths = NULL;
float sub_delay=0;
float sub_fps=0;
int sub_auto = 0;
char *vobsub_name = NULL;
int subcc_enabled=0;
int suboverlap_enabled = 1;
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;
// Needed by libmpcodecs vf_vo.c
int config_video_out(const vo_functions_t *vo, uint32_t width, uint32_t height,
uint32_t d_width, uint32_t d_height, uint32_t flags,
char *title, uint32_t format) {
return 1;
}
// Needed by libmpdemux.
int mp_input_check_interrupt(int time) {
usec_sleep(time);
return 0;
}
// Needed by getch2
void mplayer_put_key(int code)
{
}
char *current_module;
// Needed by mpcommon.c
void set_osd_subtitle(subtitle *subs) {
vo_sub = subs;
vo_osd_changed(OSDTYPE_SUBTITLE);
}
static double seek_to_sec;
static off_t seek_to_byte=0;
static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 };
static char * frameno_filename=NULL;
typedef struct {
unsigned char* start;
int in_size;
float frame_time;
int already_read;
int flush;
} s_frame_data;
static edl_record_ptr edl_records = NULL; ///< EDL entries memory area
static edl_record_ptr next_edl_record = NULL; ///< only for traversing edl_records
static short edl_muted; ///< Stores whether EDL is currently in muted mode.
static short edl_seeking; ///< When non-zero, stream is seekable.
static int edl_seek_type; ///< When non-zero, frames are discarded instead of seeking.
/* This header requires all the global variable declarations. */
#include "cfg-mencoder.h"
/* FIXME */
static void mencoder_exit(int level, const char *how)
{
if(vo_vobsub) vobsub_close(vo_vobsub);
vo_vobsub=NULL;
#if defined(__MINGW32__) || defined(__CYGWIN__)
unrar_uninit();
#endif
if (how)
mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_ExitingHow, how);
else
mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_Exiting);
exit(level);
}
static void parse_cfgfiles( m_config_t* conf )
{
char *conffile;
if (!disable_system_conf &&
m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mencoder.conf", 1) < 0)
mencoder_exit(1,MSGTR_ConfigFileError);
if (!disable_user_conf) {
if ((conffile = get_path("mencoder.conf")) == NULL) {
mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_GetpathProblem);
} else {
if (m_config_parse_config_file(conf, conffile, 1) < 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 res;
int len=total-size;
if(len>MAX_OUTBURST) len=MAX_OUTBURST;
res = mp_decode_audio(sh_audio, len);
if (res == -2)
mp_msg(MSGT_MENCODER, MSGL_FATAL, "Audio format change detected!\n"
"MEncoder currently cannot handle format changes - patches welcome!\n");
if (res < 0)
at_eof = 1;
if(len>sh_audio->a_out_buffer_len) len=sh_audio->a_out_buffer_len;
fast_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)
memmove(sh_audio->a_out_buffer,&sh_audio->a_out_buffer[len],sh_audio->a_out_buffer_len);
}
return size;
}
//---------------------------------------------------------------------------
static volatile int at_eof=0;
static volatile int interrupted=0;
static void exit_sighandler(int x){
at_eof=3;
interrupted=2; /* 1 means error */
}
static muxer_t* muxer=NULL;
static void add_subtitles(char *filename, float fps, int silent)
{
sub_data *subd;
#ifdef CONFIG_ASS
ASS_Track *asst = 0;
#endif
if (filename == NULL) return;
// try to load plain text subtilte
char *p = NULL;
int cp_index = -1;
sub_cp = sub_cps;
do {
cp_index++;
p = strchr(sub_cp, ',');
if (p) *p = 0; // temp change
subd = sub_read_file(filename, fps);
#ifdef CONFIG_ASS
if (ass_enabled)
#ifdef CONFIG_ICONV
asst = ass_read_file(ass_library, filename, sub_cp);
#else
asst = ass_read_file(ass_library, filename, 0);
#endif
if (p)
{
*p = ','; // restore
if (!subd)
sub_cp = p + 1; // to next codepage token
}
else break; // no more codepage token
} while (!subd && sub_cp && *sub_cp);
if (ass_enabled && subd && !asst)
asst = ass_read_subdata(ass_library, subd, fps);
if (!asst && !subd && !silent)
#else
if (!subd && !silent)
#endif
mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_CantLoadSub,
filename_recode(filename));
#ifdef CONFIG_ASS
if (!asst && !subd) return;
ass_track = asst;
#else
if (!subd) return;
#endif
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_FILE_SUB_FILENAME=%s\n",
filename_recode(filename));
subdata = subd;
}
/* Provide a timer value suitable for use in A/V sync calculations.
* mux->timer records the current position of the muxer stream.
* mux->encoder_delay is how many frames are currently buffered by the
* encoder. To mencoder core, encoder-buffered frames have been "dealt with"--
* they'll get to the muxer eventually. So, for the purposes of A/V sync,
* we need to add the total time length of buffered frames to the muxer stream
* position. */
static double adjusted_muxer_time(muxer_stream_t *mux)
{
if (! mux) return MP_NOPTS_VALUE;
return mux->timer + (double) mux->encoder_delay * mux->h.dwScale/mux->h.dwRate;
}
/* This function returns the absolute time for which MEncoder will switch files
* or move in the file so audio can be cut correctly. -1 if there is no limit. */
static float stop_time(demuxer_t* demuxer, muxer_stream_t* mux_v)
{
float timeleft = -1;
if (play_n_frames >= 0)
timeleft = adjusted_muxer_time(mux_v) + play_n_frames * (double)(mux_v->h.dwScale) / mux_v->h.dwRate;
if (end_at.type == END_AT_TIME && (timeleft > end_at.pos || timeleft == -1))
timeleft = end_at.pos;
if (next_edl_record && demuxer && demuxer->video) { // everything is OK to be checked
float tmp = adjusted_muxer_time(mux_v) + next_edl_record->start_sec - demuxer->video->pts;
if (timeleft == -1 || timeleft > tmp) {
// There's less time in EDL than what we already know
if (next_edl_record->action == EDL_SKIP && edl_seeking) {
timeleft = tmp;
} else if (next_edl_record->action == EDL_MUTE) {
//timeleft = next_edl_record->start_sec - demuxer->video->pts;
// For the moment (and probably forever) EDL mute doesn't work in MEncoder
}
}
}
return timeleft;
}
/** \brief Seeks audio forward to pts by dumping audio packets
* \return The current audio pts. */
static float forward_audio(float pts, demux_stream_t *d_audio, muxer_stream_t* mux_a)
{
sh_audio_t * sh_audio = d_audio ? d_audio->sh : NULL;
int samplesize, avg;
float a_pts = calc_a_pts(sh_audio, d_audio);
if (!sh_audio) return a_pts;
if (sh_audio->audio.dwScale) samplesize = sh_audio->audio.dwSampleSize;
else samplesize = (sh_audio->wf ? sh_audio->wf->nBlockAlign : 1);
avg = (sh_audio->wf ? sh_audio->wf->nAvgBytesPerSec : sh_audio->i_bps);
// after a demux_seek, a_pts will be zero until you read some audio.
// carefully checking if a_pts is truely correct by reading tiniest amount of data possible.
if (pts > a_pts && a_pts == 0.0 && samplesize) {
if (demux_read_data(sh_audio->ds,mux_a->buffer,samplesize) <= 0) return a_pts; // EOF
a_pts = calc_a_pts(sh_audio, d_audio);
}
while (pts > a_pts) {
int len;
if (samplesize) {
len = avg * (pts - a_pts > 0.5 ? 0.5 : pts - a_pts);
len/= samplesize; if(len<1) len=1;
len*= samplesize;
len = demux_read_data(sh_audio->ds,mux_a->buffer,len);
} else {
unsigned char * crap;
len = ds_get_packet(sh_audio->ds, &crap);
}
if (len <= 0) break; // EOF of audio.
a_pts = calc_a_pts(sh_audio, d_audio);
}
return a_pts;
}
/** \brief Seeks slowly by dumping frames.
* \return 1 for success, 2 for EOF. */
static int slowseek(float end_pts, demux_stream_t *d_video,
demux_stream_t *d_audio, muxer_stream_t *mux_a,
s_frame_data *frame_data, int framecopy, int print_info)
{
sh_video_t * sh_video = d_video->sh;
vf_instance_t * vfilter = sh_video ? sh_video->vfilter : NULL;
int done = 0;
while (!interrupted) {
float a_pts = 0.;
if (!frame_data->already_read) { // when called after fixdelay, a frame is already read
frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
frame_data->flush = frame_data->in_size < 0 && d_video->eof;
if (frame_data->flush) {
frame_data->in_size = 0;
frame_data->start = NULL;
}
if(frame_data->in_size<0) return 2;
sh_video->timer += frame_data->frame_time;
}
frame_data->already_read = 0;
a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
if (done) {
// wait for keyframe in case of -ovc copy
if (!framecopy || (sh_video->ds->flags & 1)) {
frame_data->already_read = 1;
return 1;
}
}
if (sh_video->pts >= end_pts) done = 1;
if (vfilter) {
int softskip = (vfilter->control(vfilter, VFCTRL_SKIP_NEXT_FRAME, 0) == CONTROL_TRUE);
void *decoded_frame = decode_video(sh_video, frame_data->start, frame_data->in_size, !softskip, MP_NOPTS_VALUE, NULL);
if (decoded_frame)
filter_video(sh_video, decoded_frame, MP_NOPTS_VALUE);
else if (frame_data->flush)
return 2;
}
if (print_info) mp_msg(MSGT_MENCODER, MSGL_STATUS,
MSGTR_EdlSkipStartEndCurrent,
next_edl_record->start_sec, next_edl_record->stop_sec,
sh_video->pts, a_pts);
}
if (interrupted) return 2;
return 1;
}
/// Deletes audio or video as told by -delay to sync
static void fixdelay(demux_stream_t *d_video, demux_stream_t *d_audio,
muxer_stream_t *mux_a, s_frame_data *frame_data,
int framecopy)
{
// TODO: Find a way to encode silence instead of deleting video
sh_video_t * sh_video = d_video->sh;
float a_pts;
// demux_seek has a weirdness that sh_video->pts is meaningless,
// until a single frame is read... Same for audio actually too.
// Reading one frame, and keeping it.
frame_data->in_size = video_read_frame(sh_video, &frame_data->frame_time, &frame_data->start, force_fps);
if(frame_data->in_size<0) return;
sh_video->timer += frame_data->frame_time;
frame_data->already_read = 1;
a_pts = forward_audio(sh_video->pts - frame_data->frame_time + audio_delay, d_audio, mux_a);
if (audio_delay > 0) return;
else if (sh_video->pts - frame_data->frame_time + audio_delay >= a_pts) return;
slowseek(a_pts - audio_delay, d_video, d_audio, mux_a, frame_data, framecopy, 0);
}
/** \brief Seeks for EDL
* \return 1 for success, 0 for failure, 2 for EOF. */
static int edl_seek(edl_record_ptr next_edl_record, demuxer_t *demuxer,
demux_stream_t *d_audio, muxer_stream_t *mux_a,
s_frame_data *frame_data, int framecopy)
{
sh_video_t * sh_video = demuxer->video ? demuxer->video->sh : NULL;
if (!sh_video) return 0;
if (sh_video->pts >= next_edl_record->stop_sec) return 1; // nothing to do...
if (!edl_seek_type) {
if(demux_seek(demuxer, next_edl_record->stop_sec - sh_video->pts, audio_delay, 0)){
sh_video->pts = demuxer->video->pts;
//if (vo_vobsub) vobsub_seek(vo_vobsub,sh_video->pts);
//if(vo_spudec) spudec_reset(vo_spudec);
if (audio_delay != 0.0) fixdelay(demuxer->video, d_audio, mux_a, frame_data, framecopy);
return 1;
}
// non-seekable stream.
return 0;
}
// slow seek, read every frame.
return slowseek(next_edl_record->stop_sec, demuxer->video, d_audio, mux_a, frame_data, framecopy, 1);
}
int main(int argc,char* argv[]){
stream_t* stream=NULL;
stream_t* ostream=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;
s_frame_data frame_data = { .start = NULL, .in_size = 0, .frame_time = 0., .already_read = 0 };
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;
// For correct deallocation
unsigned char *mux_v_buffer = NULL;
muxer_stream_t* mux_v=NULL;
off_t muxer_f_size=0;
double v_pts_corr=0;
double v_timer_corr=0;
double sub_offset=0;
int did_seek=0;
m_entry_t* filelist = NULL;
int decoded_frameno=0;
int next_frameno=-1;
int curfile=0;
int new_srate=0;
unsigned int timer_start=0;
ao_data_t ao_data = {0,0,0,0,OUTBURST,-1,0};
audio_encoding_params_t aparams;
audio_encoder_t *aencoder = NULL;
user_correct_pts = 0;
common_preinit(&argc, &argv);
#if defined(__MINGW32__) || defined(__CYGWIN__)
unrar_init();
#endif
// Create the config context and register the options
mconfig = m_config_new();
m_config_register_options(mconfig,mencoder_opts);
m_config_register_options(mconfig, common_opts);
// Preparse the command line
m_config_preparse_command_line(mconfig,argc,argv);
print_version("MEncoder");
parse_cfgfiles(mconfig);
filelist = m_config_parse_me_command_line(mconfig, argc, argv);
if(!filelist) mencoder_exit(1, MSGTR_ErrorParsingCommandLine);
if (!common_init())
mencoder_exit(1,NULL);
{
char *extension;
if (!out_filename) mencoder_exit(1,MSGTR_MissingOutputFilename);
extension=strrchr(out_filename,'.');
if (extension != NULL && strlen(extension) > 3 && strlen(extension) < 6)
{
extension++;
switch (out_file_format)
{
case MUXER_TYPE_AVI:
if (strcasecmp(extension,"avi"))
mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_MencoderWrongFormatAVI);
break;
case MUXER_TYPE_MPEG:
if (strcasecmp(extension,"mpg") &&
strcasecmp(extension,"mpeg") &&
strcasecmp(extension,"vob"))
mp_msg(MSGT_MENCODER, MSGL_WARN, MSGTR_MencoderWrongFormatMPG);
break;
}
}
}
/* Display what configure line was used */
mp_msg(MSGT_MENCODER, MSGL_V, "Configuration: " CONFIGURATION "\n");
if (frameno_filename) {
stream2=open_stream(frameno_filename, NULL, NULL);
if(stream2){
demuxer2=demux_open(stream2,DEMUXER_TYPE_AVI,-1,-1,-2,NULL);
if(demuxer2) mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_UsingPass3ControlFile, frameno_filename);
else mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_FormatNotRecognized);
}
}
/* HACK, for some weird reason, push() has to be called twice,
otherwise options are not saved correctly */
m_config_push(mconfig);
play_next_file:
m_config_push(mconfig);
m_entry_set_options(mconfig,&filelist[curfile]);
filename = filelist[curfile].name;
if(!filename){
mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_MissingFilename);
mencoder_exit(1,NULL);
}
if (vobsub_name)
vo_vobsub = vobsub_open(vobsub_name, spudec_ifo, 1, &vo_spudec);
#ifdef CONFIG_ASS
// must be before demuxer open, since the settings are
// used in generating the ASSTrack
if (ass_enabled && ass_library)
ass_mp_reset_config(ass_library);
#endif
stream=open_stream(filename,0,&file_format);
if(!stream){
mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_CannotOpenFile_Device);
mencoder_exit(1,NULL);
}
mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_OpenedStream, file_format, (int)(stream->start_pos), (int)(stream->end_pos));
if(stream->type==STREAMTYPE_BD){
if(audio_lang && audio_id==-1) audio_id=bd_aid_from_lang(stream,audio_lang);
if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=bd_sid_from_lang(stream,dvdsub_lang);
}
#ifdef CONFIG_LIBBLURAY
if(stream->type==STREAMTYPE_BLURAY){
if(audio_lang && audio_id==-1) audio_id=bluray_id_from_lang(stream,stream_ctrl_audio,audio_lang);
if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=bluray_id_from_lang(stream,stream_ctrl_sub,dvdsub_lang);
}
#endif
#ifdef CONFIG_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
#ifdef CONFIG_DVDNAV
if(stream->type==STREAMTYPE_DVDNAV){
if(audio_lang && audio_id==-1) audio_id=mp_dvdnav_aid_from_lang(stream,audio_lang);
if(dvdsub_lang && dvdsub_id==-1) dvdsub_id=mp_dvdnav_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*1024ull,0,0);
if(demuxer2) audio_id=-2; /* do NOT read audio packets... */
demuxer=demux_open(stream,file_format,audio_id,video_id,dvdsub_id,filename);
if(!demuxer){
mp_msg(MSGT_DEMUXER, MSGL_FATAL, MSGTR_FormatNotRecognized);
mp_msg(MSGT_DEMUXER, MSGL_FATAL, MSGTR_CannotOpenDemuxer);
mencoder_exit(1,NULL);
}
if (ts_prog) {
demux_program_t prog = { .progid = ts_prog };
if (demux_control(demuxer, DEMUXER_CTRL_IDENTIFY_PROGRAM, &prog) != DEMUXER_CTRL_NOTIMPL) {
audio_id = prog.aid; // switching is handled by select_audio below
video_id = prog.vid; // switching is handled by select_video below
}
}
select_video(demuxer, video_id);
select_audio(demuxer, audio_id, audio_lang);
if (dvdsub_id == -1 && dvdsub_lang)
dvdsub_id = demuxer_sub_track_by_lang(demuxer, dvdsub_lang);
if (dvdsub_id == -1)
dvdsub_id = demuxer_default_sub_track(demuxer);
for (i = 0; i < MAX_S_STREAMS; i++) {
sh_sub_t *sh = demuxer->s_streams[i];
if (sh && sh->sid == dvdsub_id) {
demuxer->sub->id = i;
demuxer->sub->sh = sh;
break;
}
}
if(dvd_chapter>1) {
float pts;
if (demuxer_seek_chapter(demuxer, dvd_chapter-1, 1, &pts, NULL, NULL) >= 0 && pts > -1.0)
seek_to_sec = pts;
}
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_CPLAYER, MSGL_FATAL, MSGTR_CannotReadVideoProperties);
mencoder_exit(1,NULL);
}
mp_msg(MSGT_MENCODER,MSGL_INFO, MSGTR_FilefmtFourccSizeFpsFtime,
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_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 || playback_speed != 1.0)){
// 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");
}
if (sh_audio) {
new_srate = sh_audio->samplerate;
if (playback_speed != 1.0) {
new_srate *= playback_speed;
// limits are taken from libaf/af_resample.c
if (new_srate < 8000) new_srate = 8000;
if (new_srate > 192000) new_srate = 192000;
playback_speed = (float)new_srate / (float)sh_audio->samplerate;
}
}
// set up video encoder:
if (!curfile) { // curfile is non zero when a second file is opened
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 CONFIG_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 (!vo_spudec) {
init_vo_spudec(stream, sh_video, d_dvdsub ? d_dvdsub->sh : NULL);
}
ostream = open_output_stream(out_filename, 0);
if(!ostream) {
mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CannotOpenOutputFile, out_filename);
mencoder_exit(1,NULL);
}
muxer=muxer_new_muxer(out_file_format,ostream);
if(!muxer) {
mp_msg(MSGT_MENCODER, MSGL_FATAL, MSGTR_CannotInitializeMuxer);
mencoder_exit(1,NULL);
}
#if 0
//disabled: it horrybly distorts filtered sound
if(out_file_format == MUXER_TYPE_MPEG) audio_preload = 0;
#endif
muxer->audio_delay_fix = audio_delay_fix;
// ============= VIDEO ===============
mux_v=muxer_new_stream(muxer,MUXER_TYPE_VIDEO);
mux_v->buffer_size=0x800000; // 8MB
mux_v->buffer=mux_v_buffer=malloc(mux_v->buffer_size);
mux_v->source=sh_video;
mux_v->h.dwSampleSize=0; // VBR
#ifdef CONFIG_FFMPEG
{
double fps = force_ofps?force_ofps:sh_video->fps*playback_speed;
AVRational q= av_d2q(fps, fps*1001+2);
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*playback_speed);
#endif
mux_v->codec=out_video_codec;
mux_v->bih=NULL;
}
sh_video->codec=NULL;
sh_video->vfilter=NULL; // fixme!
switch(mux_v->codec){
case VCODEC_COPY:
if (!curfile) {
if (sh_video->bih) {
mux_v->bih=malloc(sh_video->bih->biSize);
memcpy(mux_v->bih, sh_video->bih, sh_video->bih->biSize);
}
else
{
mux_v->bih=calloc(1,sizeof(*mux_v->bih));
mux_v->bih->biSize=sizeof(*mux_v->bih);
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_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
mux_v->bih->biWidth, mux_v->bih->biHeight,
mux_v->bih->biBitCount, mux_v->bih->biCompression);
if (curfile) {
if (sh_video->bih) {
if ((mux_v->bih->biSize != sh_video->bih->biSize) ||
memcmp(mux_v->bih, sh_video->bih, sh_video->bih->biSize))
{
mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
sh_video->bih->biWidth, sh_video->bih->biHeight,
sh_video->bih->biBitCount, sh_video->bih->biCompression);
mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_FrameCopyFileMismatch);
mencoder_exit(1,NULL);
}
}
else {
if ((mux_v->bih->biWidth != sh_video->disp_w) ||
(mux_v->bih->biHeight != sh_video->disp_h) ||
(mux_v->bih->biCompression != sh_video->format)) {
mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_VCodecFramecopy,
sh_video->disp_w, sh_video->disp_w, 24, sh_video->format);
mp_msg(MSGT_MENCODER,MSGL_FATAL,MSGTR_FrameCopyFileMismatch);
mencoder_exit(1,NULL);
}
}
}
break;
case VCODEC_FRAMENO:
if (!curfile) {
mux_v->bih=calloc(1,sizeof(*mux_v->bih));
mux_v->bih->biSize=sizeof(*mux_v->bih);
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: {
static vf_instance_t * ve = NULL;
if (!ve) {
switch(mux_v->codec){
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;