forked from huceke/omxplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
omxplayer.cpp
1193 lines (1050 loc) · 36 KB
/
omxplayer.cpp
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
/*
*
* Copyright (C) 2012 Edgar Hucek
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <stdint.h>
#include <termios.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <getopt.h>
#include <string.h>
#define AV_NOWARN_DEPRECATED
extern "C" {
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
};
#include "OMXStreamInfo.h"
#include "utils/log.h"
#include "DllAvUtil.h"
#include "DllAvFormat.h"
#include "DllAvFilter.h"
#include "DllAvCodec.h"
#include "linux/RBP.h"
#include "OMXVideo.h"
#include "OMXAudioCodecOMX.h"
#include "utils/PCMRemap.h"
#include "OMXClock.h"
#include "OMXAudio.h"
#include "OMXReader.h"
#include "OMXPlayerVideo.h"
#include "OMXPlayerAudio.h"
#include "OMXPlayerSubtitles.h"
#include "DllOMX.h"
#include "Srt.h"
#include <string>
#include <utility>
typedef enum {CONF_FLAGS_FORMAT_NONE, CONF_FLAGS_FORMAT_SBS, CONF_FLAGS_FORMAT_TB } FORMAT_3D_T;
enum PCMChannels *m_pChannelMap = NULL;
volatile sig_atomic_t g_abort = false;
bool m_bMpeg = false;
bool m_passthrough = false;
long m_initialVolume = 0;
bool m_Deinterlace = false;
bool m_HWDecode = false;
std::string deviceString = "";
int m_use_hw_audio = false;
std::string m_external_subtitles_path;
bool m_has_external_subtitles = false;
std::string m_font_path = "/usr/share/fonts/truetype/freefont/FreeSans.ttf";
bool m_has_font = false;
float m_font_size = 0.055f;
bool m_centered = false;
unsigned int m_subtitle_lines = 3;
bool m_Pause = false;
OMXReader m_omx_reader;
int m_audio_index_use = -1;
int m_seek_pos = 0;
bool m_buffer_empty = true;
bool m_thread_player = false;
OMXClock *m_av_clock = NULL;
COMXStreamInfo m_hints_audio;
COMXStreamInfo m_hints_video;
OMXPacket *m_omx_pkt = NULL;
bool m_hdmi_clock_sync = false;
bool m_no_hdmi_clock_sync = false;
bool m_stop = false;
int m_subtitle_index = -1;
DllBcmHost m_BcmHost;
OMXPlayerVideo m_player_video;
OMXPlayerAudio m_player_audio;
OMXPlayerSubtitles m_player_subtitles;
int m_tv_show_info = 0;
bool m_has_video = false;
bool m_has_audio = false;
bool m_has_subtitle = false;
float m_display_aspect = 0.0f;
bool m_boost_on_downmix = false;
bool m_loop = false;
enum{ERROR=-1,SUCCESS,ONEBYTE};
static struct termios orig_termios;
static void restore_termios()
{
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
}
static int orig_fl;
static void restore_fl()
{
fcntl(STDIN_FILENO, F_SETFL, orig_fl);
}
void sig_handler(int s)
{
printf("strg-c catched\n");
signal(SIGINT, SIG_DFL);
g_abort = true;
}
void print_usage()
{
printf("Usage: omxplayer [OPTIONS] [FILE]\n");
printf("Options :\n");
printf(" -h / --help print this help\n");
// printf(" -a / --alang language audio language : e.g. ger\n");
printf(" -n / --aidx index audio stream index : e.g. 1\n");
printf(" -o / --adev device audio out device : e.g. hdmi/local\n");
printf(" -i / --info dump stream format and exit\n");
printf(" -s / --stats pts and buffer stats\n");
printf(" -p / --passthrough audio passthrough\n");
printf(" -d / --deinterlace deinterlacing\n");
printf(" -w / --hw hw audio decoding\n");
printf(" -3 / --3d mode switch tv into 3d mode (e.g. SBS/TB)\n");
printf(" -y / --hdmiclocksync adjust display refresh rate to match video (default)\n");
printf(" -z / --nohdmiclocksync do not adjust display refresh rate to match video\n");
printf(" -t / --sid index show subtitle with index\n");
printf(" -r / --refresh adjust framerate/resolution to video\n");
printf(" -l / --pos start position (in seconds)\n");
printf(" --boost-on-downmix boost volume when downmixing\n");
printf(" --loop repeat content\n");
printf(" --vol Set initial volume in millibels (default 0)\n");
printf(" --subtitles path external subtitles in UTF-8 srt format\n");
printf(" --font path subtitle font\n");
printf(" (default: /usr/share/fonts/truetype/freefont/FreeSans.ttf)\n");
printf(" --font-size size font size as thousandths of screen height\n");
printf(" (default: 55)\n");
printf(" --align left/center subtitle alignment (default: left)\n");
printf(" --lines n number of lines to accommodate in the subtitle buffer\n");
printf(" (default: 3)\n");
printf(" --win \"x1 y1 x2 y2\" Set position of video window\n");
}
void PrintSubtitleInfo()
{
auto count = m_omx_reader.SubtitleStreamCount();
size_t index = 0;
if(m_has_external_subtitles)
{
++count;
if(!m_player_subtitles.GetUseExternalSubtitles())
index = m_player_subtitles.GetActiveStream() + 1;
}
else if(m_has_subtitle)
{
index = m_player_subtitles.GetActiveStream();
}
printf("Subtitle count: %d, state: %s, index: %d, delay: %d\n",
count,
m_has_subtitle && m_player_subtitles.GetVisible() ? " on" : "off",
index+1,
m_has_subtitle ? m_player_subtitles.GetDelay() : 0);
}
void SetSpeed(int iSpeed)
{
if(!m_av_clock)
return;
if(iSpeed < OMX_PLAYSPEED_PAUSE)
return;
m_omx_reader.SetSpeed(iSpeed);
if(m_av_clock->OMXPlaySpeed() != OMX_PLAYSPEED_PAUSE && iSpeed == OMX_PLAYSPEED_PAUSE)
m_Pause = true;
else if(m_av_clock->OMXPlaySpeed() == OMX_PLAYSPEED_PAUSE && iSpeed != OMX_PLAYSPEED_PAUSE)
m_Pause = false;
m_av_clock->OMXSpeed(iSpeed);
}
static float get_display_aspect_ratio(HDMI_ASPECT_T aspect)
{
float display_aspect;
switch (aspect) {
case HDMI_ASPECT_4_3: display_aspect = 4.0/3.0; break;
case HDMI_ASPECT_14_9: display_aspect = 14.0/9.0; break;
case HDMI_ASPECT_16_9: display_aspect = 16.0/9.0; break;
case HDMI_ASPECT_5_4: display_aspect = 5.0/4.0; break;
case HDMI_ASPECT_16_10: display_aspect = 16.0/10.0; break;
case HDMI_ASPECT_15_9: display_aspect = 15.0/9.0; break;
case HDMI_ASPECT_64_27: display_aspect = 64.0/27.0; break;
default: display_aspect = 16.0/9.0; break;
}
return display_aspect;
}
static float get_display_aspect_ratio(SDTV_ASPECT_T aspect)
{
float display_aspect;
switch (aspect) {
case SDTV_ASPECT_4_3: display_aspect = 4.0/3.0; break;
case SDTV_ASPECT_14_9: display_aspect = 14.0/9.0; break;
case SDTV_ASPECT_16_9: display_aspect = 16.0/9.0; break;
default: display_aspect = 4.0/3.0; break;
}
return display_aspect;
}
void FlushStreams(double pts)
{
// if(m_av_clock)
// m_av_clock->OMXPause();
if(m_has_video)
m_player_video.Flush();
if(m_has_audio)
m_player_audio.Flush();
if(m_has_subtitle)
m_player_subtitles.Flush(pts);
if(m_omx_pkt)
{
m_omx_reader.FreePacket(m_omx_pkt);
m_omx_pkt = NULL;
}
if(pts != DVD_NOPTS_VALUE)
m_av_clock->OMXUpdateClock(pts);
// if(m_av_clock)
// {
// m_av_clock->OMXReset();
// m_av_clock->OMXResume();
// }
}
void SetVideoMode(int width, int height, int fpsrate, int fpsscale, FORMAT_3D_T is3d)
{
int32_t num_modes = 0;
int i;
HDMI_RES_GROUP_T prefer_group;
HDMI_RES_GROUP_T group = HDMI_RES_GROUP_CEA;
float fps = 60.0f; // better to force to higher rate if no information is known
uint32_t prefer_mode;
if (fpsrate && fpsscale)
fps = DVD_TIME_BASE / OMXReader::NormalizeFrameduration((double)DVD_TIME_BASE * fpsscale / fpsrate);
//Supported HDMI CEA/DMT resolutions, preferred resolution will be returned
TV_SUPPORTED_MODE_NEW_T *supported_modes = NULL;
// query the number of modes first
int max_supported_modes = m_BcmHost.vc_tv_hdmi_get_supported_modes_new(group, NULL, 0, &prefer_group, &prefer_mode);
if (max_supported_modes > 0)
supported_modes = new TV_SUPPORTED_MODE_NEW_T[max_supported_modes];
if (supported_modes)
{
num_modes = m_BcmHost.vc_tv_hdmi_get_supported_modes_new(group,
supported_modes, max_supported_modes, &prefer_group, &prefer_mode);
CLog::Log(LOGDEBUG, "EGL get supported modes (%d) = %d, prefer_group=%x, prefer_mode=%x\n",
group, num_modes, prefer_group, prefer_mode);
}
TV_SUPPORTED_MODE_NEW_T *tv_found = NULL;
if (num_modes > 0 && prefer_group != HDMI_RES_GROUP_INVALID)
{
uint32_t best_score = 1<<30;
uint32_t scan_mode = 0;
for (i=0; i<num_modes; i++)
{
TV_SUPPORTED_MODE_NEW_T *tv = supported_modes + i;
uint32_t score = 0;
uint32_t w = tv->width;
uint32_t h = tv->height;
uint32_t r = tv->frame_rate;
/* Check if frame rate match (equal or exact multiple) */
if(fabs(r - 1.0f*fps) / fps < 0.002f)
score += 0;
else if(fabs(r - 2.0f*fps) / fps < 0.002f)
score += 1<<8;
else
score += (1<<28)/r; // bad - but prefer higher framerate
/* Check size too, only choose, bigger resolutions */
if(width && height)
{
/* cost of too small a resolution is high */
score += max((int)(width -w), 0) * (1<<16);
score += max((int)(height-h), 0) * (1<<16);
/* cost of too high a resolution is lower */
score += max((int)(w-width ), 0) * (1<<4);
score += max((int)(h-height), 0) * (1<<4);
}
// native is good
if (!tv->native)
score += 1<<16;
// interlace is bad
if (scan_mode != tv->scan_mode)
score += (1<<16);
// wanting 3D but not getting it is a negative
if (is3d == CONF_FLAGS_FORMAT_SBS && !(tv->struct_3d_mask & HDMI_3D_STRUCT_SIDE_BY_SIDE_HALF_HORIZONTAL))
score += 1<<18;
if (is3d == CONF_FLAGS_FORMAT_TB && !(tv->struct_3d_mask & HDMI_3D_STRUCT_TOP_AND_BOTTOM))
score += 1<<18;
// prefer square pixels modes
float par = get_display_aspect_ratio((HDMI_ASPECT_T)tv->aspect_ratio)*(float)tv->height/(float)tv->width;
score += fabs(par - 1.0f) * (1<<12);
/*printf("mode %dx%d@%d %s%s:%x par=%.2f score=%d\n", tv->width, tv->height,
tv->frame_rate, tv->native?"N":"", tv->scan_mode?"I":"", tv->code, par, score);*/
if (score < best_score)
{
tv_found = tv;
best_score = score;
}
}
}
if(tv_found)
{
printf("Output mode %d: %dx%d@%d %s%s:%x\n", tv_found->code, tv_found->width, tv_found->height,
tv_found->frame_rate, tv_found->native?"N":"", tv_found->scan_mode?"I":"", tv_found->code);
// if we are closer to ntsc version of framerate, let gpu know
int ifps = (int)(fps+0.5f);
bool ntsc_freq = fabs(fps*1001.0f/1000.0f - ifps) < fabs(fps-ifps);
char response[80];
vc_gencmd(response, sizeof response, "hdmi_ntsc_freqs %d", ntsc_freq);
/* inform TV of any 3D settings. Note this property just applies to next hdmi mode change, so no need to call for 2D modes */
HDMI_PROPERTY_PARAM_T property;
property.property = HDMI_PROPERTY_3D_STRUCTURE;
property.param1 = HDMI_3D_FORMAT_NONE;
property.param2 = 0;
if (is3d != CONF_FLAGS_FORMAT_NONE)
{
if (is3d == CONF_FLAGS_FORMAT_SBS && tv_found->struct_3d_mask & HDMI_3D_STRUCT_SIDE_BY_SIDE_HALF_HORIZONTAL)
property.param1 = HDMI_3D_FORMAT_SBS_HALF;
else if (is3d == CONF_FLAGS_FORMAT_TB && tv_found->struct_3d_mask & HDMI_3D_STRUCT_TOP_AND_BOTTOM)
property.param1 = HDMI_3D_FORMAT_TB_HALF;
m_BcmHost.vc_tv_hdmi_set_property(&property);
}
printf("ntsc_freq:%d %s%s\n", ntsc_freq, property.param1 == HDMI_3D_FORMAT_SBS_HALF ? "3DSBS":"", property.param1 == HDMI_3D_FORMAT_TB_HALF ? "3DTB":"");
m_BcmHost.vc_tv_hdmi_power_on_explicit_new(HDMI_MODE_HDMI, (HDMI_RES_GROUP_T)group, tv_found->code);
}
}
bool Exists(const std::string& path)
{
struct stat buf;
auto error = stat(path.c_str(), &buf);
return !error || errno != ENOENT;
}
bool IsURL(const std::string& str)
{
auto result = str.find("://");
if(result == std::string::npos || result == 0)
return false;
for(size_t i = 0; i < result; ++i)
{
if(!isalpha(str[i]))
return false;
}
return true;
}
int main(int argc, char *argv[])
{
signal(SIGINT, sig_handler);
if (isatty(STDIN_FILENO))
{
struct termios new_termios;
tcgetattr(STDIN_FILENO, &orig_termios);
new_termios = orig_termios;
new_termios.c_lflag &= ~(ICANON | ECHO | ECHOCTL | ECHONL);
new_termios.c_cflag |= HUPCL;
new_termios.c_cc[VMIN] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
atexit(restore_termios);
}
else
{
orig_fl = fcntl(STDIN_FILENO, F_GETFL);
fcntl(STDIN_FILENO, F_SETFL, orig_fl | O_NONBLOCK);
atexit(restore_fl);
}
std::string m_filename, m_filename_format;
char m_fn_buf[255];
double m_incr = 0;
CRBP g_RBP;
COMXCore g_OMX;
bool m_stats = false;
bool m_dump_format = false;
FORMAT_3D_T m_3d = CONF_FLAGS_FORMAT_NONE;
bool m_refresh = false;
double startpts = 0;
double loop_offset = 0;
int loop_index = 0;
CRect DestRect = {0,0,0,0};
TV_DISPLAY_STATE_T tv_state;
const int font_opt = 0x100;
const int font_size_opt = 0x101;
const int align_opt = 0x102;
const int subtitles_opt = 0x103;
const int lines_opt = 0x104;
const int pos_opt = 0x105;
const int vol_opt = 0x106;
const int boost_on_downmix_opt = 0x200;
const int loop_opt = 0x201;
struct option longopts[] = {
{ "info", no_argument, NULL, 'i' },
{ "help", no_argument, NULL, 'h' },
{ "aidx", required_argument, NULL, 'n' },
{ "adev", required_argument, NULL, 'o' },
{ "stats", no_argument, NULL, 's' },
{ "passthrough", no_argument, NULL, 'p' },
{ "vol", required_argument, NULL, vol_opt },
{ "deinterlace", no_argument, NULL, 'd' },
{ "hw", no_argument, NULL, 'w' },
{ "3d", required_argument, NULL, '3' },
{ "hdmiclocksync", no_argument, NULL, 'y' },
{ "nohdmiclocksync", no_argument, NULL, 'z' },
{ "refresh", no_argument, NULL, 'r' },
{ "sid", required_argument, NULL, 't' },
{ "pos", required_argument, NULL, 'l' },
{ "font", required_argument, NULL, font_opt },
{ "font-size", required_argument, NULL, font_size_opt },
{ "align", required_argument, NULL, align_opt },
{ "subtitles", required_argument, NULL, subtitles_opt },
{ "lines", required_argument, NULL, lines_opt },
{ "win", required_argument, NULL, pos_opt },
{ "boost-on-downmix", no_argument, NULL, boost_on_downmix_opt },
{ "loop", no_argument, NULL, loop_opt },
{ 0, 0, 0, 0 }
};
int c;
std::string mode;
while ((c = getopt_long(argc, argv, "wihn:l:o:cslpd3:yzt:r", longopts, NULL)) != -1)
{
switch (c)
{
case 'r':
m_refresh = true;
break;
case 'y':
m_hdmi_clock_sync = true;
break;
case 'z':
m_no_hdmi_clock_sync = true;
break;
case '3':
mode = optarg;
if(mode != "SBS" && mode != "TB")
{
print_usage();
return 0;
}
if(mode == "TB")
m_3d = CONF_FLAGS_FORMAT_TB;
else
m_3d = CONF_FLAGS_FORMAT_SBS;
break;
case 'd':
m_Deinterlace = true;
break;
case 'w':
m_use_hw_audio = true;
break;
case 'p':
m_passthrough = true;
break;
case 's':
m_stats = true;
break;
case 'o':
deviceString = optarg;
if(deviceString != "local" && deviceString != "hdmi")
{
print_usage();
return 0;
}
deviceString = "omx:" + deviceString;
break;
case 'i':
m_dump_format = true;
break;
case 't':
m_subtitle_index = atoi(optarg) - 1;
if(m_subtitle_index < 0)
m_subtitle_index = 0;
break;
case 'n':
m_audio_index_use = atoi(optarg) - 1;
if(m_audio_index_use < 0)
m_audio_index_use = 0;
break;
case 'l':
m_seek_pos = atoi(optarg) ;
if (m_seek_pos < 0)
m_seek_pos = 0;
break;
case font_opt:
m_font_path = optarg;
m_has_font = true;
break;
case font_size_opt:
{
const int thousands = atoi(optarg);
if (thousands > 0)
m_font_size = thousands*0.001f;
}
break;
case align_opt:
m_centered = !strcmp(optarg, "center");
break;
case subtitles_opt:
m_external_subtitles_path = optarg;
m_has_external_subtitles = true;
break;
case lines_opt:
m_subtitle_lines = std::max(atoi(optarg), 1);
break;
case pos_opt:
sscanf(optarg, "%f %f %f %f", &DestRect.x1, &DestRect.y1, &DestRect.x2, &DestRect.y2);
break;
case vol_opt:
m_initialVolume = atoi(optarg);
break;
case boost_on_downmix_opt:
m_boost_on_downmix = true;
break;
case loop_opt:
m_loop = true;
break;
case 0:
break;
case 'h':
print_usage();
return 0;
break;
case ':':
return 0;
break;
default:
return 0;
break;
}
}
if (optind >= argc) {
print_usage();
return 0;
}
m_filename = argv[optind];
auto PrintFileNotFound = [](const std::string& path)
{
printf("File \"%s\" not found.\n", path.c_str());
};
bool filename_is_URL = IsURL(m_filename);
if(!filename_is_URL && !Exists(m_filename))
{
if (m_loop)
{
// Second chance - formatted filename (multipart loop)
m_filename_format = m_filename;
snprintf(m_fn_buf, 255, m_filename_format.c_str(), loop_index++);
m_filename = m_fn_buf;
if (!Exists(m_filename))
{
PrintFileNotFound(m_filename);
return 0;
}
}
else
{
PrintFileNotFound(m_filename);
return 0;
}
}
if(m_has_font && !Exists(m_font_path))
{
PrintFileNotFound(m_font_path);
return 0;
}
if(m_has_external_subtitles && !Exists(m_external_subtitles_path))
{
PrintFileNotFound(m_external_subtitles_path);
return 0;
}
if(!m_has_external_subtitles && !filename_is_URL)
{
auto subtitles_path = m_filename.substr(0, m_filename.find_last_of(".")) +
".srt";
if(Exists(subtitles_path))
{
m_external_subtitles_path = subtitles_path;
m_has_external_subtitles = true;
}
}
CLog::Init("./");
g_RBP.Initialize();
g_OMX.Initialize();
m_av_clock = new OMXClock();
m_thread_player = true;
if(!m_omx_reader.Open(m_filename.c_str(), m_dump_format))
goto do_exit;
if(m_dump_format)
goto do_exit;
m_bMpeg = m_omx_reader.IsMpegVideo();
m_has_video = m_omx_reader.VideoStreamCount();
m_has_audio = m_omx_reader.AudioStreamCount();
m_has_subtitle = m_has_external_subtitles ||
m_omx_reader.SubtitleStreamCount();
if(m_filename.find("3DSBS") != string::npos || m_filename.find("HSBS") != string::npos)
m_3d = CONF_FLAGS_FORMAT_SBS;
else if(m_filename.find("3DTAB") != string::npos || m_filename.find("HTAB") != string::npos)
m_3d = CONF_FLAGS_FORMAT_TB;
// 3d modes don't work without switch hdmi mode
if (m_3d != CONF_FLAGS_FORMAT_NONE)
m_refresh = true;
// you really don't want want to match refresh rate without hdmi clock sync
if (m_refresh && !m_no_hdmi_clock_sync)
m_hdmi_clock_sync = true;
if(!m_av_clock->OMXInitialize(m_has_video, m_has_audio))
goto do_exit;
if(m_hdmi_clock_sync && !m_av_clock->HDMIClockSync())
goto do_exit;
m_omx_reader.GetHints(OMXSTREAM_AUDIO, m_hints_audio);
m_omx_reader.GetHints(OMXSTREAM_VIDEO, m_hints_video);
if(m_audio_index_use != -1)
m_omx_reader.SetActiveStream(OMXSTREAM_AUDIO, m_audio_index_use);
if(m_has_video && m_refresh)
{
memset(&tv_state, 0, sizeof(TV_DISPLAY_STATE_T));
m_BcmHost.vc_tv_get_display_state(&tv_state);
SetVideoMode(m_hints_video.width, m_hints_video.height, m_hints_video.fpsrate, m_hints_video.fpsscale, m_3d);
}
// get display aspect
TV_DISPLAY_STATE_T current_tv_state;
memset(¤t_tv_state, 0, sizeof(TV_DISPLAY_STATE_T));
m_BcmHost.vc_tv_get_display_state(¤t_tv_state);
if(current_tv_state.state & ( VC_HDMI_HDMI | VC_HDMI_DVI )) {
//HDMI or DVI on
m_display_aspect = get_display_aspect_ratio((HDMI_ASPECT_T)current_tv_state.display.hdmi.aspect_ratio);
} else {
//composite on
m_display_aspect = get_display_aspect_ratio((SDTV_ASPECT_T)current_tv_state.display.sdtv.display_options.aspect);
}
m_display_aspect *= (float)current_tv_state.display.hdmi.height/(float)current_tv_state.display.hdmi.width;
// seek on start
if (m_seek_pos !=0 && m_omx_reader.CanSeek()) {
printf("Seeking start of video to %i seconds\n", m_seek_pos);
m_omx_reader.SeekTime(m_seek_pos * 1000.0f, 0, &startpts); // from seconds to DVD_TIME_BASE
}
if(m_has_video && !m_player_video.Open(m_hints_video, m_av_clock, DestRect, m_Deinterlace, m_bMpeg,
m_hdmi_clock_sync, m_thread_player, m_display_aspect))
goto do_exit;
{
std::vector<Subtitle> external_subtitles;
if(m_has_external_subtitles &&
!ReadSrt(m_external_subtitles_path, external_subtitles))
{
puts("Unable to read the subtitle file.");
goto do_exit;
}
if(m_has_subtitle &&
!m_player_subtitles.Open(m_omx_reader.SubtitleStreamCount(),
std::move(external_subtitles),
m_font_path,
m_font_size,
m_centered,
m_subtitle_lines,
m_av_clock))
goto do_exit;
}
if(m_has_subtitle)
{
if(!m_has_external_subtitles)
{
if(m_subtitle_index != -1)
{
m_player_subtitles.SetActiveStream(
std::min(m_subtitle_index, m_omx_reader.SubtitleStreamCount()-1));
}
m_player_subtitles.SetUseExternalSubtitles(false);
}
if(m_subtitle_index == -1 && !m_has_external_subtitles)
m_player_subtitles.SetVisible(false);
}
m_omx_reader.GetHints(OMXSTREAM_AUDIO, m_hints_audio);
if (deviceString == "")
{
if (m_BcmHost.vc_tv_hdmi_audio_supported(EDID_AudioFormat_ePCM, 2, EDID_AudioSampleRate_e44KHz, EDID_AudioSampleSize_16bit ) == 0)
deviceString = "omx:hdmi";
else
deviceString = "omx:local";
}
if(m_has_audio && !m_player_audio.Open(m_hints_audio, m_av_clock, &m_omx_reader, deviceString,
m_passthrough, m_initialVolume, m_use_hw_audio,
m_boost_on_downmix, m_thread_player))
goto do_exit;
m_av_clock->SetSpeed(DVD_PLAYSPEED_NORMAL);
m_av_clock->OMXStateExecute();
m_av_clock->OMXStart(0.0);
struct timespec starttime, endtime;
PrintSubtitleInfo();
while(!m_stop)
{
int ch[8];
int chnum = 0;
if(g_abort)
goto do_exit;
while((ch[chnum] = getchar()) != EOF) chnum++;
if (chnum > 1) ch[0] = ch[chnum - 1] | (ch[chnum - 2] << 8);
switch(ch[0])
{
case 'z':
m_tv_show_info = !m_tv_show_info;
vc_tv_show_info(m_tv_show_info);
break;
case '1':
SetSpeed(m_av_clock->OMXPlaySpeed() - 1);
break;
case '2':
SetSpeed(m_av_clock->OMXPlaySpeed() + 1);
break;
case 'j':
if(m_has_audio)
{
int new_index = m_omx_reader.GetAudioIndex() - 1;
if (new_index >= 0)
m_omx_reader.SetActiveStream(OMXSTREAM_AUDIO, new_index);
}
break;
case 'k':
if(m_has_audio)
m_omx_reader.SetActiveStream(OMXSTREAM_AUDIO, m_omx_reader.GetAudioIndex() + 1);
break;
case 'i':
if(m_omx_reader.GetChapterCount() > 0)
{
m_omx_reader.SeekChapter(m_omx_reader.GetChapter() - 1, &startpts);
FlushStreams(startpts);
}
else
{
m_incr = -600.0;
}
break;
case 'o':
if(m_omx_reader.GetChapterCount() > 0)
{
m_omx_reader.SeekChapter(m_omx_reader.GetChapter() + 1, &startpts);
FlushStreams(startpts);
}
else
{
m_incr = 600.0;
}
break;
case 'n':
if(m_has_subtitle)
{
if(!m_player_subtitles.GetUseExternalSubtitles())
{
if (m_player_subtitles.GetActiveStream() == 0)
{
if(m_has_external_subtitles)
m_player_subtitles.SetUseExternalSubtitles(true);
}
else
{
m_player_subtitles.SetActiveStream(
m_player_subtitles.GetActiveStream()-1);
}
}
m_player_subtitles.SetVisible(true);
PrintSubtitleInfo();
}
break;
case 'm':
if(m_has_subtitle)
{
if(m_player_subtitles.GetUseExternalSubtitles())
{
if(m_omx_reader.SubtitleStreamCount())
{
assert(m_player_subtitles.GetActiveStream() == 0);
m_player_subtitles.SetUseExternalSubtitles(false);
}
}
else
{
auto new_index = m_player_subtitles.GetActiveStream()+1;
if(new_index < (size_t) m_omx_reader.SubtitleStreamCount())
m_player_subtitles.SetActiveStream(new_index);
}
m_player_subtitles.SetVisible(true);
PrintSubtitleInfo();
}
break;
case 's':
if(m_has_subtitle)
{
m_player_subtitles.SetVisible(!m_player_subtitles.GetVisible());
PrintSubtitleInfo();
}
break;
case 'd':
if(m_has_subtitle && m_player_subtitles.GetVisible())
{
m_player_subtitles.SetDelay(m_player_subtitles.GetDelay() - 250);
PrintSubtitleInfo();
}
break;
case 'f':
if(m_has_subtitle && m_player_subtitles.GetVisible())
{
m_player_subtitles.SetDelay(m_player_subtitles.GetDelay() + 250);
PrintSubtitleInfo();
}
break;
case 'q': case 27:
m_stop = true;
goto do_exit;
break;
case 0x5b44: // key left
if(m_omx_reader.CanSeek()) m_incr = -30.0;
break;
case 0x5b43: // key right
if(m_omx_reader.CanSeek()) m_incr = 30.0;
break;
case 0x5b41: // key up
if(m_omx_reader.CanSeek()) m_incr = 600.0;
break;
case 0x5b42: // key down
if(m_omx_reader.CanSeek()) m_incr = -600.0;
break;
case ' ':
case 'p':
m_Pause = !m_Pause;
if(m_Pause)
{
SetSpeed(OMX_PLAYSPEED_PAUSE);
m_av_clock->OMXPause();
if(m_has_subtitle)
m_player_subtitles.Pause();
}
else
{
if(m_has_subtitle)
m_player_subtitles.Resume();
SetSpeed(OMX_PLAYSPEED_NORMAL);
m_av_clock->OMXResume();
}
break;
case '-':
m_player_audio.SetCurrentVolume(m_player_audio.GetCurrentVolume() - 300);
printf("Current Volume: %.2fdB\n", m_player_audio.GetCurrentVolume() / 100.0f);
break;
case '+': case '=':
m_player_audio.SetCurrentVolume(m_player_audio.GetCurrentVolume() + 300);
printf("Current Volume: %.2fdB\n", m_player_audio.GetCurrentVolume() / 100.0f);
break;
default:
break;
}
if(m_Pause)
{
OMXClock::OMXSleep(2);
continue;
}
if(m_incr != 0 && !m_bMpeg)
{
int seek_flags = 0;
double seek_pos = 0;
double pts = 0;
if(m_has_subtitle)
m_player_subtitles.Pause();
m_av_clock->OMXStop();
pts = m_av_clock->GetPTS() - loop_offset;
seek_pos = (pts / DVD_TIME_BASE) + m_incr;
seek_flags = m_incr < 0.0f ? AVSEEK_FLAG_BACKWARD : 0;
seek_pos *= 1000.0f;
m_incr = 0;
if(m_omx_reader.SeekTime(seek_pos, seek_flags, &startpts))
FlushStreams(startpts);
m_player_video.Close();
if(m_has_video && !m_player_video.Open(m_hints_video, m_av_clock, DestRect, m_Deinterlace, m_bMpeg,
m_hdmi_clock_sync, m_thread_player, m_display_aspect))
goto do_exit;
m_av_clock->OMXStart(startpts);
if(m_has_subtitle)
m_player_subtitles.Resume();
loop_offset = 0;
}
/* player got in an error state */
if(m_player_audio.Error())
{
printf("audio player error. emergency exit!!!\n");
goto do_exit;