forked from popcornmix/omxplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
omxplayer.cpp
1555 lines (1386 loc) · 54.5 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 "OMXControl.h"
#include "DllOMX.h"
#include "Srt.h"
#include "KeyConfig.h"
#include "utils/Strprintf.h"
#include "Keyboard.h"
#include <string>
#include <utility>
#include "version.h"
// when we repeatedly seek, rather than play continuously
#define TRICKPLAY(speed) (speed < 0 || speed > 4 * DVD_PLAYSPEED_NORMAL)
#define DISPLAY_TEXT(text, ms) if(m_osd) m_player_subtitles.DisplayText(text, ms)
#define DISPLAY_TEXT_SHORT(text) DISPLAY_TEXT(text, 1000)
#define DISPLAY_TEXT_LONG(text) DISPLAY_TEXT(text, 2000)
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_passthrough = false;
long m_Volume = 0;
long m_Amplification = 0;
bool m_Deinterlace = false;
bool m_NoDeinterlace = false;
bool m_HWDecode = false;
std::string deviceString = "";
int m_use_hw_audio = false;
bool m_osd = true;
std::string m_external_subtitles_path;
bool m_has_external_subtitles = false;
std::string m_font_path = "/usr/share/fonts/truetype/freefont/FreeSans.ttf";
std::string m_italic_font_path = "/usr/share/fonts/truetype/freefont/FreeSansOblique.ttf";
bool m_asked_for_font = false;
bool m_asked_for_italic_font = false;
float m_font_size = 0.055f;
bool m_centered = false;
bool m_ghost_box = true;
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_thread_player = false;
OMXClock *m_av_clock = NULL;
OMXControl m_omxcontrol;
Keyboard m_keyboard;
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_gen_log = false;
enum{ERROR=-1,SUCCESS,ONEBYTE};
void sig_handler(int s)
{
if (s==SIGINT && !g_abort)
{
signal(SIGINT, SIG_DFL);
g_abort = true;
return;
}
signal(SIGABRT, SIG_DFL);
signal(SIGSEGV, SIG_DFL);
signal(SIGFPE, SIG_DFL);
abort();
}
void print_usage()
{
printf("Usage: omxplayer [OPTIONS] [FILE]\n");
printf("Options :\n");
printf(" -h / --help print this help\n");
printf(" -v / --version print version info\n");
printf(" -k / --keys print key bindings\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 force deinterlacing\n");
printf(" --nodeinterlace force no 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(" -g / --genlog generate log file\n");
printf(" -l / --pos n start position (in seconds)\n");
printf(" -b / --blank set background to black\n");
printf(" --boost-on-downmix boost volume when downmixing\n");
printf(" --vol n Set initial volume in millibels (default 0)\n");
printf(" --amp n Set initial amplification in millibels (default 0)\n");
printf(" --no-osd do not display status information on screen\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(" --italic-font path (default: /usr/share/fonts/truetype/freefont/FreeSansOblique.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(" --no-ghost-box no semitransparent boxes behind subtitles\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");
printf(" --audio_fifo n Size of audio output fifo in seconds\n");
printf(" --video_fifo n Size of video output fifo in MB\n");
printf(" --audio_queue n Size of audio input queue in MB\n");
printf(" --video_queue n Size of video input queue in MB\n");
printf(" --threshold n Amount of buffered data required to come out of buffering in seconds\n");
printf(" --key-config <file> Uses key bindings specified in <file> instead of the default\n");
}
void print_keybindings()
{
printf("Key bindings :\n");
printf(" 1 decrease speed\n");
printf(" 2 increase speed\n");
printf(" < rewind\n");
printf(" > fast forward\n");
printf(" z show info\n");
printf(" j previous audio stream\n");
printf(" k next audio stream\n");
printf(" i previous chapter\n");
printf(" o next chapter\n");
printf(" n previous subtitle stream\n");
printf(" m next subtitle stream\n");
printf(" s toggle subtitles\n");
printf(" d decrease subtitle delay (- 250 ms)\n");
printf(" f increase subtitle delay (+ 250 ms)\n");
printf(" q exit omxplayer\n");
printf(" p / space pause/resume\n");
printf(" - decrease volume\n");
printf(" + / = increase volume\n");
printf(" left arrow seek -30 seconds\n");
printf(" right arrow seek +30 seconds\n");
printf(" down arrow seek -600 seconds\n");
printf(" up arrow seek +600 seconds\n");
}
void print_version()
{
printf("omxplayer - Commandline multimedia player for the Raspberry Pi\n");
printf(" Build date: %s\n", VERSION_DATE);
printf(" Version : %s [%s]\n", VERSION_HASH, VERSION_BRANCH);
printf(" Repository: %s\n", VERSION_REPO);
}
static 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);
}
static void FlushStreams(double pts);
static void SetSpeed(int iSpeed)
{
if(!m_av_clock)
return;
m_omx_reader.SetSpeed(iSpeed);
// flush when in trickplay mode
if (TRICKPLAY(iSpeed) || TRICKPLAY(m_av_clock->OMXPlaySpeed()))
FlushStreams(DVD_NOPTS_VALUE);
m_av_clock->OMXSetSpeed(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;
}
static void FlushStreams(double pts)
{
m_av_clock->OMXStop();
m_av_clock->OMXPause();
if(m_has_video)
m_player_video.Flush();
if(m_has_audio)
m_player_audio.Flush();
if(pts != DVD_NOPTS_VALUE)
m_av_clock->OMXMediaTime(0.0);
if(m_has_subtitle)
m_player_subtitles.Flush();
if(m_omx_pkt)
{
m_omx_reader.FreePacket(m_omx_pkt);
m_omx_pkt = NULL;
}
}
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);
if(m_gen_log) {
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);
}
if (supported_modes)
delete[] supported_modes;
}
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;
}
bool IsPipe(const std::string& str)
{
if (str.compare(0, 5, "pipe:") == 0)
return true;
return false;
}
static int get_mem_gpu(void)
{
char response[80] = "";
int gpu_mem = 0;
if (vc_gencmd(response, sizeof response, "get_mem gpu") == 0)
vc_gencmd_number_property(response, "gpu", &gpu_mem);
return gpu_mem;
}
static void blank_background(bool enable)
{
if (!enable)
return;
// we create a 1x1 black pixel image that is added to display just behind video
DISPMANX_DISPLAY_HANDLE_T display;
DISPMANX_UPDATE_HANDLE_T update;
DISPMANX_RESOURCE_HANDLE_T resource;
DISPMANX_ELEMENT_HANDLE_T element;
int ret;
uint32_t vc_image_ptr;
VC_IMAGE_TYPE_T type = VC_IMAGE_RGB565;
uint16_t image = 0x0000; // black
VC_RECT_T dst_rect, src_rect;
display = vc_dispmanx_display_open(0);
assert(display);
resource = vc_dispmanx_resource_create( type, 1 /*width*/, 1 /*height*/, &vc_image_ptr );
assert( resource );
vc_dispmanx_rect_set( &dst_rect, 0, 0, 1, 1);
ret = vc_dispmanx_resource_write_data( resource, type, sizeof(image), &image, &dst_rect );
assert(ret == 0);
vc_dispmanx_rect_set( &src_rect, 0, 0, 1<<16, 1<<16);
vc_dispmanx_rect_set( &dst_rect, 0, 0, 0, 0);
update = vc_dispmanx_update_start(0);
assert(update);
element = vc_dispmanx_element_add(update, display, -1 /*layer*/, &dst_rect, resource, &src_rect,
DISPMANX_PROTECTION_NONE, NULL, NULL, (DISPMANX_TRANSFORM_T)0 );
assert(element);
ret = vc_dispmanx_update_submit_sync( update );
assert( ret == 0 );
}
int main(int argc, char *argv[])
{
signal(SIGSEGV, sig_handler);
signal(SIGABRT, sig_handler);
signal(SIGFPE, sig_handler);
signal(SIGINT, sig_handler);
bool m_send_eos = false;
bool m_packet_after_seek = false;
bool m_seek_flush = false;
std::string m_filename;
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;
CRect DestRect = {0,0,0,0};
bool m_blank_background = false;
bool sentStarted = false;
float audio_fifo_size = 0.0; // zero means use default
float video_fifo_size = 0.0;
float audio_queue_size = 0.0;
float video_queue_size = 0.0;
float m_threshold = 0.2f; // amount of audio/video required to come out of buffering
TV_DISPLAY_STATE_T tv_state;
const int font_opt = 0x100;
const int italic_font_opt = 0x201;
const int font_size_opt = 0x101;
const int align_opt = 0x102;
const int no_ghost_box_opt = 0x203;
const int subtitles_opt = 0x103;
const int lines_opt = 0x104;
const int pos_opt = 0x105;
const int vol_opt = 0x106;
const int audio_fifo_opt = 0x107;
const int video_fifo_opt = 0x108;
const int audio_queue_opt = 0x109;
const int video_queue_opt = 0x10a;
const int no_deinterlace_opt = 0x10b;
const int threshold_opt = 0x10c;
const int boost_on_downmix_opt = 0x200;
const int key_config_opt = 0x10d;
const int amp_opt = 0x10e;
const int no_osd_opt = 0x202;
struct option longopts[] = {
{ "info", no_argument, NULL, 'i' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{ "keys", no_argument, NULL, 'k' },
{ "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 },
{ "amp", required_argument, NULL, amp_opt },
{ "deinterlace", no_argument, NULL, 'd' },
{ "nodeinterlace",no_argument, NULL, no_deinterlace_opt },
{ "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' },
{ "genlog", no_argument, NULL, 'g' },
{ "sid", required_argument, NULL, 't' },
{ "pos", required_argument, NULL, 'l' },
{ "blank", no_argument, NULL, 'b' },
{ "font", required_argument, NULL, font_opt },
{ "italic-font", required_argument, NULL, italic_font_opt },
{ "font-size", required_argument, NULL, font_size_opt },
{ "align", required_argument, NULL, align_opt },
{ "no-ghost-box", no_argument, NULL, no_ghost_box_opt },
{ "subtitles", required_argument, NULL, subtitles_opt },
{ "lines", required_argument, NULL, lines_opt },
{ "win", required_argument, NULL, pos_opt },
{ "audio_fifo", required_argument, NULL, audio_fifo_opt },
{ "video_fifo", required_argument, NULL, video_fifo_opt },
{ "audio_queue", required_argument, NULL, audio_queue_opt },
{ "video_queue", required_argument, NULL, video_queue_opt },
{ "threshold", required_argument, NULL, threshold_opt },
{ "boost-on-downmix", no_argument, NULL, boost_on_downmix_opt },
{ "key-config", required_argument, NULL, key_config_opt },
{ "no-osd", no_argument, NULL, no_osd_opt },
{ 0, 0, 0, 0 }
};
#define S(x) (int)(DVD_PLAYSPEED_NORMAL*(x))
int playspeeds[] = {S(0), S(1/16.0), S(1/8.0), S(1/4.0), S(1/2.0), S(0.975), S(1.0), S(1.125), S(-32.0), S(-16.0), S(-8.0), S(-4), S(-2), S(-1), S(1), S(2.0), S(4.0), S(8.0), S(16.0), S(32.0)};
const int playspeed_slow_min = 0, playspeed_slow_max = 7, playspeed_rew_max = 8, playspeed_rew_min = 13, playspeed_normal = 14, playspeed_ff_min = 15, playspeed_ff_max = 19;
int playspeed_current = 4;
double m_last_check_time = 0.0;
int c;
std::string mode;
//Build default keymap just in case the --key-config option isn't used
map<int,int> keymap = KeyConfig::buildDefaultKeymap();
while ((c = getopt_long(argc, argv, "wihvkn:l:o:cslbpd3:yzt:rg", longopts, NULL)) != -1)
{
switch (c)
{
case 'r':
m_refresh = true;
break;
case 'g':
m_gen_log = 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 no_deinterlace_opt:
m_NoDeinterlace = 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 no_osd_opt:
m_osd = false;
break;
case font_opt:
m_font_path = optarg;
m_asked_for_font = true;
break;
case italic_font_opt:
m_italic_font_path = optarg;
m_asked_for_italic_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 no_ghost_box_opt:
m_ghost_box = false;
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_Volume = atoi(optarg);
break;
case amp_opt:
m_Amplification = atoi(optarg);
break;
case boost_on_downmix_opt:
m_boost_on_downmix = true;
break;
case audio_fifo_opt:
audio_fifo_size = atof(optarg);
break;
case video_fifo_opt:
video_fifo_size = atof(optarg);
break;
case audio_queue_opt:
audio_queue_size = atof(optarg);
break;
case video_queue_opt:
video_queue_size = atof(optarg);
break;
case threshold_opt:
m_threshold = atof(optarg);
break;
case 'b':
m_blank_background = true;
break;
case key_config_opt:
keymap = KeyConfig::parseConfigFile(optarg);
break;
case 0:
break;
case 'h':
print_usage();
return 0;
break;
case 'v':
print_version();
return 0;
break;
case 'k':
print_keybindings();
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 && !IsPipe(m_filename) && !Exists(m_filename))
{
PrintFileNotFound(m_filename);
return 0;
}
if(m_asked_for_font && !Exists(m_font_path))
{
PrintFileNotFound(m_font_path);
return 0;
}
if(m_asked_for_italic_font && !Exists(m_italic_font_path))
{
PrintFileNotFound(m_italic_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;
}
}
bool m_audio_extension = false;
const CStdString m_musicExtensions = ".nsv|.m4a|.flac|.aac|.strm|.pls|.rm|.rma|.mpa|.wav|.wma|.ogg|.mp3|.mp2|.m3u|.mod|.amf|.669|.dmf|.dsm|.far|.gdm|"
".imf|.it|.m15|.med|.okt|.s3m|.stm|.sfx|.ult|.uni|.xm|.sid|.ac3|.dts|.cue|.aif|.aiff|.wpl|.ape|.mac|.mpc|.mp+|.mpp|.shn|.zip|.rar|"
".wv|.nsf|.spc|.gym|.adx|.dsp|.adp|.ymf|.ast|.afc|.hps|.xsp|.xwav|.waa|.wvs|.wam|.gcm|.idsp|.mpdsp|.mss|.spt|.rsd|.mid|.kar|.sap|"
".cmc|.cmr|.dmc|.mpt|.mpd|.rmt|.tmc|.tm8|.tm2|.oga|.url|.pxml|.tta|.rss|.cm3|.cms|.dlt|.brstm|.wtv|.mka";
if (m_filename.find_last_of(".") != string::npos)
{
CStdString extension = m_filename.substr(m_filename.find_last_of("."));
if (!extension.IsEmpty() && m_musicExtensions.Find(extension.ToLower()) != -1)
m_audio_extension = true;
}
if(m_gen_log) {
CLog::SetLogLevel(LOG_LEVEL_DEBUG);
CLog::Init("./");
} else {
CLog::SetLogLevel(LOG_LEVEL_NONE);
}
g_RBP.Initialize();
g_OMX.Initialize();
blank_background(m_blank_background);
int gpu_mem = get_mem_gpu();
int min_gpu_mem = 64;
if (gpu_mem > 0 && gpu_mem < min_gpu_mem)
printf("Only %dM of gpu_mem is configured. Try running \"sudo raspi-config\" and ensure that \"memory_split\" has a value of %d or greater\n", gpu_mem, min_gpu_mem);
m_av_clock = new OMXClock();
m_omxcontrol.init(m_av_clock, &m_player_audio);
m_keyboard.setKeymap(keymap);
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_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_audio_extension)
{
CLog::Log(LOGWARNING, "%s - Ignoring video in audio filetype:%s", __FUNCTION__, m_filename.c_str());
m_has_video = false;
}
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())
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, false, &startpts); // from seconds to DVD_TIME_BASE
}
if(m_has_video && !m_player_video.Open(m_hints_video, m_av_clock, DestRect, m_Deinterlace ? VS_DEINTERLACEMODE_FORCE:m_NoDeinterlace ? VS_DEINTERLACEMODE_OFF:VS_DEINTERLACEMODE_AUTO,
m_hdmi_clock_sync, m_thread_player, m_display_aspect, video_queue_size, video_fifo_size))
goto do_exit;
if(m_has_subtitle || m_osd)
{
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_player_subtitles.Open(m_omx_reader.SubtitleStreamCount(),
std::move(external_subtitles),
m_font_path,
m_italic_font_path,
m_font_size,
m_centered,
m_ghost_box,
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_hints_audio.codec == CODEC_ID_AC3 || m_hints_audio.codec == CODEC_ID_EAC3) &&
m_BcmHost.vc_tv_hdmi_audio_supported(EDID_AudioFormat_eAC3, 2, EDID_AudioSampleRate_e44KHz, EDID_AudioSampleSize_16bit ) != 0)
m_passthrough = false;
if (m_hints_audio.codec == CODEC_ID_DTS &&
m_BcmHost.vc_tv_hdmi_audio_supported(EDID_AudioFormat_eDTS, 2, EDID_AudioSampleRate_e44KHz, EDID_AudioSampleSize_16bit ) != 0)
m_passthrough = false;
if(m_has_audio && !m_player_audio.Open(m_hints_audio, m_av_clock, &m_omx_reader, deviceString,
m_passthrough, m_use_hw_audio,
m_boost_on_downmix, m_thread_player, audio_queue_size, audio_fifo_size))
goto do_exit;
if(m_has_audio)
{
m_player_audio.SetVolume(pow(10, m_Volume / 2000.0));
if (m_Amplification)
m_player_audio.SetDynamicRangeCompression(m_Amplification);
}
m_av_clock->OMXPause();
m_av_clock->OMXStateExecute();