-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.c
3266 lines (2956 loc) · 112 KB
/
main.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
/*
* vim: ts=4
*
* DreamDVD V0.9 - DVD-Player for Dreambox
* Copyright (C) 2007 by Seddi
* Updates 2012-2013 Mirakels
*
* This DVD Player is based upon the great work from the libdvdnav project,
* a52dec library, ffmpeg and the knowledge from all the people who made
* watching DVD within linux possible.
*
* DreamDVD 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.
*
* DreamDVD 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* part of libdreamdvd
*/
#include "main.h"
#include "mpegaudioenc.h"
#include "a52dec.h"
#include "string.h"
#include "errno.h"
#define Debug(level, str, ...) (DebugLevel > level ? printf("LIBDVD: %07.3f: " str, (float) ddvd_get_time() / 1000.0, ##__VA_ARGS__) : 0)
#define Perror(msg) Debug(-1, "%s: %s", msg, strerror(errno))
int DebugLevel = 1;
/*
* local helper functions
*/
static ssize_t safe_write(int fd, const void *buf, size_t count)
{
const uint8_t *ptr = buf;
size_t written = 0;
ssize_t n;
while (written < count) {
n = write(fd, &ptr[written], count - written);
if (n < 0) {
if (errno != EINTR) {
Perror("write");
return written ? written : -1;
}
}
else
written += n;
}
return written;
}
static void write_string(const char *filename, const char *string)
{
FILE *f;
f = fopen(filename, "w");
if (f == NULL) {
Perror(filename);
return;
}
fputs(string, f);
fclose(f);
}
static int open_pipe(int fd[2])
{
int flags;
fd[0] = fd[1] = -1;
if (pipe(fd) < 0) {
Perror("pipe");
goto err;
}
flags = fcntl(fd[0], F_GETFL);
if (flags < 0) {
Perror("F_GETFL");
goto err;
}
if (fcntl(fd[0], F_SETFL, flags | O_NONBLOCK) < 0) {
Perror("F_SETFL");
goto err;
}
return 0;
err:
if (fd[0] != -1) {
close(fd[0]);
fd[0] = -1;
}
if (fd[1] != -1) {
close(fd[1]);
fd[1] = -1;
}
return -1;
}
/*
* external functions
*/
// create ddvd handle and set defaults
struct ddvd *ddvd_create(void)
{
struct ddvd *pconfig;
int i;
// Debug(2, "ddvd_create: setup dvd config struct\n");
pconfig = malloc(sizeof(struct ddvd));
if (pconfig == NULL) {
Perror("malloc pconfig");
return NULL;
}
memset(pconfig, 0, sizeof(struct ddvd));
for (i = 0; i < MAX_AUDIO; i++)
pconfig->audio_format[i] = -1;
pconfig->last_audio_id = -1;
for (i = 0; i < MAX_SPU; i++)
pconfig->spu_map[i].logical_id = pconfig->spu_map[i].stream_id = pconfig->spu_map[i].lang = -1;
pconfig->last_spu_id = -1;
// defaults
ddvd_set_ac3thru(pconfig, 0);
ddvd_set_language(pconfig, "en");
ddvd_set_dvd_path(pconfig, "/dev/cdroms/cdrom0");
ddvd_set_video(pconfig, DDVD_4_3, DDVD_LETTERBOX, DDVD_PAL);
ddvd_set_lfb(pconfig, NULL, 720, 576, 1, 720);
struct ddvd_resume resume_info;
resume_info.title = resume_info.chapter = resume_info.block = resume_info.audio_id =
resume_info.audio_lock = resume_info.spu_id = resume_info.spu_lock = 0;
ddvd_set_resume_pos(pconfig, resume_info);
pconfig->should_resume = 0;
pconfig->next_time_update = 0;
strcpy(pconfig->title_string, "");
// open pipes
if (open_pipe(pconfig->key_pipe) < 0) {
ddvd_close(pconfig);
return NULL;
}
if (open_pipe(pconfig->message_pipe) < 0) {
ddvd_close(pconfig);
return NULL;
}
return pconfig;
}
// destroy ddvd handle
void ddvd_close(struct ddvd *pconfig)
{
// Debug(2, "ddvd_close: cleanup dvd config struct\n");
if (pconfig->message_pipe[0] != -1)
close(pconfig->message_pipe[0]);
if (pconfig->message_pipe[1] != -1)
close(pconfig->message_pipe[1]);
if (pconfig->key_pipe[0] != -1)
close(pconfig->key_pipe[0]);
if (pconfig->key_pipe[1] != -1)
close(pconfig->key_pipe[1]);
if (pconfig->dvd_path != NULL)
free(pconfig->dvd_path);
free(pconfig);
}
// get message_pipe fd for polling functions in the host app
int ddvd_get_messagepipe_fd(struct ddvd *pconfig)
{
return pconfig->message_pipe[0];
}
// set resume position
// At this point the ddvd structure might not yet be completely setup
// Setup 'event' that will be picked up in the main loop.
void ddvd_set_resume_pos(struct ddvd *pconfig, struct ddvd_resume resume_info)
{
Debug(2, "ddvd_set_resume_pos: title=%d, chapter=%d, block=%lu, audio_id=%d, audio_lock=%d, spu_id=%d, spu_lock=%d\n",
resume_info.title, resume_info.chapter, resume_info.block, resume_info.audio_id, resume_info.audio_lock,
resume_info.spu_id, resume_info.spu_lock);
pconfig->should_resume = 1;
pconfig->resume_title = resume_info.title;
pconfig->resume_chapter = resume_info.chapter;
pconfig->resume_block = resume_info.block;
pconfig->resume_audio_id = resume_info.audio_id;
pconfig->resume_audio_lock = resume_info.audio_lock;
pconfig->resume_spu_id = resume_info.spu_id;
pconfig->resume_spu_lock = resume_info.spu_lock;
}
// set framebuffer options
void ddvd_set_lfb(struct ddvd *pconfig, unsigned char *lfb, int xres, int yres, int bypp, int stride)
{
return ddvd_set_lfb_ex(pconfig, lfb, xres, yres, bypp, stride, 0);
}
void ddvd_set_lfb_ex(struct ddvd *pconfig, unsigned char *lfb, int xres, int yres, int bypp, int stride, int canscale)
{
pconfig->lfb = lfb;
pconfig->xres = xres;
pconfig->yres = yres;
pconfig->stride = stride;
pconfig->bypp = bypp;
pconfig->canscale = canscale;
}
// set path to dvd block device or file structure/iso
void ddvd_set_dvd_path(struct ddvd *pconfig, const char *path)
{
if (pconfig->dvd_path != NULL)
free(pconfig->dvd_path);
pconfig->dvd_path = strdup(path);
}
// set language
void ddvd_set_language(struct ddvd *pconfig, const char lang[2])
{
Debug(2, "ddvd_set_language to %c%c\n", lang[0], lang[1]);
memcpy(pconfig->language, lang, 2);
}
// set internal ac3 decoding (needs liba52 which will be dynamically loaded)
void ddvd_set_ac3thru(struct ddvd *pconfig, int ac3thru)
{
pconfig->ac3thru = ac3thru;
}
// set video options
void ddvd_set_video_ex(struct ddvd *pconfig, int aspect, int tv_mode, int tv_mode2, int tv_system)
{
pconfig->aspect = aspect;
pconfig->tv_mode = tv_mode;
pconfig->tv_mode2 = tv_mode2;
pconfig->tv_system = tv_system;
}
// set subtitle stream id
void ddvd_set_spu(struct ddvd *pconfig, int spu_id)
{
ddvd_send_key(pconfig, DDVD_SET_SUBTITLE);
ddvd_send_key(pconfig, spu_id);
}
// set audio stream id
void ddvd_set_audio(struct ddvd *pconfig, int audio_id)
{
ddvd_send_key(pconfig, DDVD_SET_AUDIO);
ddvd_send_key(pconfig, audio_id);
}
// set video options
void ddvd_set_video(struct ddvd *pconfig, int aspect, int tv_mode, int tv_system)
{
ddvd_set_video_ex(pconfig, aspect, tv_mode, tv_mode, tv_system);
}
// send commands/keys to the main player
void ddvd_send_key(struct ddvd *pconfig, int key)
{
safe_write(pconfig->key_pipe[1], &key, sizeof(int));
}
// skip n seconds in playing n>0 forward - n<0 backward
void ddvd_skip_seconds(struct ddvd *pconfig, int seconds)
{
ddvd_send_key(pconfig, seconds < 0 ? DDVD_SKIP_BWD : DDVD_SKIP_FWD);
ddvd_send_key(pconfig, seconds);
}
// jump to beginning of given title
void ddvd_set_title(struct ddvd *pconfig, int title)
{
ddvd_send_key(pconfig, DDVD_SET_TITLE);
ddvd_send_key(pconfig, title);
}
// jump to beginning of given chapter
void ddvd_set_chapter(struct ddvd *pconfig, int chapter)
{
ddvd_send_key(pconfig, DDVD_SET_CHAPTER);
ddvd_send_key(pconfig, chapter);
}
// get and process the next message from the main player
int ddvd_get_next_message(struct ddvd *pconfig, int blocked)
{
int res;
int i;
if (ddvd_readpipe(pconfig->message_pipe[0], &res, sizeof(int), blocked) != sizeof(int))
res = DDVD_NULL;
switch (res) { // more data to process ?
case DDVD_COLORTABLE_UPDATE:
for (i = 0; i < 4; i++)
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_col[i], sizeof(struct ddvd_color), 1);
break;
case DDVD_SHOWOSD_TIME:
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_time, sizeof(pconfig->last_time), 1);
break;
case DDVD_SHOWOSD_STATE_FFWD:
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_trickspeed, sizeof(pconfig->last_trickspeed), 1);
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_time, sizeof(pconfig->last_time), 1);
break;
case DDVD_SHOWOSD_STATE_FBWD:
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_trickspeed, sizeof(pconfig->last_trickspeed), 1);
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_time, sizeof(pconfig->last_time), 1);
break;
case DDVD_SHOWOSD_STRING:
ddvd_readpipe(pconfig->message_pipe[0], pconfig->last_string, sizeof(pconfig->last_string), 1);
break;
case DDVD_SHOWOSD_AUDIO:
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_audio_id, sizeof(int), 1);
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_audio_lang, sizeof(uint16_t), 1);
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_audio_type, sizeof(int), 1);
break;
case DDVD_SHOWOSD_SUBTITLE:
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_spu_id, sizeof(int), 1);
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_spu_lang, sizeof(uint16_t), 1);
break;
case DDVD_SCREEN_UPDATE:
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->blit_area, sizeof(pconfig->blit_area), 1);
break;
case DDVD_SHOWOSD_ANGLE:
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->angle_current, sizeof(int), 1);
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->angle_num, sizeof(int), 1);
break;
case DDVD_SIZE_CHANGED:
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_size, sizeof(struct ddvd_size_evt), 1);
break;
case DDVD_PROGRESSIVE_CHANGED:
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_progressive, sizeof(struct ddvd_progressive_evt), 1);
break;
case DDVD_FRAMERATE_CHANGED:
ddvd_readpipe(pconfig->message_pipe[0], &pconfig->last_framerate, sizeof(struct ddvd_framerate_evt), 1);
break;
default:
break;
}
return res;
}
// get last blit area
void ddvd_get_last_blit_area(struct ddvd *pconfig, int *x_start, int *x_end, int *y_start, int *y_end)
{
struct ddvd_resize_return *ptr = &pconfig->blit_area;
memcpy(x_start, &ptr->x_start, sizeof(int));
memcpy(x_end, &ptr->x_end, sizeof(int));
memcpy(y_start, &ptr->y_start, sizeof(int));
memcpy(y_end, &ptr->y_end, sizeof(int));
}
void ddvd_get_blit_destination(struct ddvd *pconfig, int *x_offset, int *y_offset, int *width, int *height)
{
struct ddvd_resize_return *ptr = &pconfig->blit_area;
*x_offset = ptr->x_offset;
*y_offset = ptr->y_offset;
*width = ptr->width;
*height = ptr->height;
}
// get angle info
void ddvd_get_angle_info(struct ddvd*pconfig, int *current, int *num)
{
memcpy(current, &pconfig->angle_current, sizeof(pconfig->angle_current));
memcpy(num, &pconfig->angle_num, sizeof(pconfig->angle_num));
}
// get last colortable for 8bit mode (4 colors)
void ddvd_get_last_colortable(struct ddvd *pconfig, void *colortable)
{
memcpy(colortable, pconfig->last_col, sizeof(pconfig->last_col));
}
// get last received playing time as struct ddvd_time
void ddvd_get_last_time(struct ddvd *pconfig, void *timestamp)
{
memcpy(timestamp, &pconfig->last_time, sizeof(pconfig->last_time));
}
// get the actual trickspeed (2-64x) when in trickmode
void ddvd_get_last_trickspeed(struct ddvd *pconfig, void *trickspeed)
{
memcpy(trickspeed, &pconfig->last_trickspeed, sizeof(pconfig->last_trickspeed));
}
// get last text message from player
void ddvd_get_last_string(struct ddvd *pconfig, void *text)
{
memcpy(text, pconfig->last_string, sizeof(pconfig->last_string));
}
// get the number of available audio tracks
void ddvd_get_audio_count(struct ddvd *pconfig, void *count)
{
int c = 0;
int i;
for (i = 0; i < MAX_AUDIO; i++) {
if (pconfig->audio_format[i] != -1)
c++;
}
memcpy(count, &c, sizeof(int));
}
// get the active audio track
void ddvd_get_last_audio(struct ddvd *pconfig, void *id, void *lang, void *type)
{
memcpy(id, &pconfig->last_audio_id, sizeof(pconfig->last_audio_id));
memcpy(lang, &pconfig->last_audio_lang, sizeof(pconfig->last_audio_lang));
memcpy(type, &pconfig->last_audio_type, sizeof(pconfig->last_audio_type));
Debug(2, "ddvd_get_last_audio id=%d\n", * (int *)id);
}
// get audio track details for given audio track id
void ddvd_get_audio_byid(struct ddvd *pconfig, int audio_id, void *lang, void *type)
{
int audio_id_logical;
uint16_t audio_lang = 0xFFFF;
audio_id_logical = dvdnav_get_audio_logical_stream(dvdnav, audio_id);
audio_lang = dvdnav_audio_stream_to_lang(dvdnav, audio_id_logical);
if (audio_lang == 0xFFFF)
audio_lang = 0x2D2D;
memcpy(lang, &audio_lang, sizeof(uint16_t));
memcpy(type, &pconfig->audio_format[audio_id], sizeof(int));
Debug(2, "ddvd_get_audio_byid %d %c%c\n", audio_id, audio_lang >> 8, audio_lang & 0xff);
}
// get the active SPU track
void ddvd_get_last_spu(struct ddvd *pconfig, void *id, void *lang)
{
memcpy(id, &pconfig->last_spu_id, sizeof(pconfig->last_spu_id));
memcpy(lang, &pconfig->last_spu_lang, sizeof(pconfig->last_spu_lang));
Debug(2, "ddvd_get_last_spu id=%d\n", * (int *)id);
}
// get the number of available subtitle tracks
void ddvd_get_spu_count(struct ddvd *pconfig, void *count)
{
int c = 0;
int i;
for (i = 0; i < MAX_SPU; i++) {
if (pconfig->spu_map[i].logical_id != -1)
c++;
}
memcpy(count, &c, sizeof(int));
Debug(2, "ddvd_get_spu_count %d streams\n", c);
}
// get language details for given subtitle track id
void ddvd_get_spu_byid(struct ddvd *pconfig, int spu_id, void *lang)
{
uint16_t spu_lang = 0xFFFF;
if (spu_id < MAX_SPU && pconfig->spu_map[spu_id].logical_id > -1)
spu_lang = pconfig->spu_map[spu_id].lang;
memcpy(lang, &spu_lang, sizeof(uint16_t));
Debug(2, "ddvd_get_spu_byid %d: %d %d %c%c\n", spu_id, pconfig->spu_map[spu_id].logical_id,
pconfig->spu_map[spu_id].stream_id, spu_lang >> 8, spu_lang & 0xff);
}
// get dvd title string
void ddvd_get_title_string(struct ddvd *pconfig, char *title_string)
{
memcpy(title_string, pconfig->title_string, sizeof(pconfig->title_string));
}
// get actual position for resuming
void ddvd_get_resume_pos(struct ddvd *pconfig, struct ddvd_resume *resume_info)
{
resume_info->title = pconfig->resume_title;
resume_info->chapter = pconfig->resume_chapter;
resume_info->block = pconfig->resume_block;
resume_info->audio_id = pconfig->resume_audio_id;
resume_info->audio_lock = pconfig->resume_audio_lock;
resume_info->spu_id = pconfig->resume_spu_id;
resume_info->spu_lock = pconfig->resume_spu_lock;
}
void ddvd_get_last_size(struct ddvd *pconfig, int *width, int *height, int *aspect)
{
*width = pconfig->last_size.width;
*height = pconfig->last_size.height;
*aspect = pconfig->last_size.aspect;
}
void ddvd_get_last_progressive(struct ddvd *pconfig, int *progressive)
{
*progressive = pconfig->last_progressive.progressive;
}
void ddvd_get_last_framerate(struct ddvd *pconfig, int *framerate)
{
*framerate = pconfig->last_framerate.framerate;
}
static int calc_x_scale_offset(int dvd_aspect, int tv_mode, int tv_mode2, int tv_aspect)
{
int x_offset=0;
if (dvd_aspect == 0 && tv_mode == DDVD_PAN_SCAN) {
switch (tv_aspect) {
case DDVD_16_10:
x_offset = (ddvd_screeninfo_xres - ddvd_screeninfo_xres * 12 / 15) / 2; // correct 16:10 (broadcom 15:9) panscan (pillarbox) overlay
break;
case DDVD_16_9:
x_offset = (ddvd_screeninfo_xres - ddvd_screeninfo_xres * 3 / 4) / 2; // correct 16:9 panscan (pillarbox) overlay
default:
break;
}
}
if (dvd_aspect >= 2 && tv_aspect == DDVD_4_3 && tv_mode == DDVD_PAN_SCAN)
x_offset = -(ddvd_screeninfo_xres * 4 / 3 - ddvd_screeninfo_xres) / 2;
if (dvd_aspect >= 2 && tv_aspect == DDVD_16_10 && tv_mode2 == DDVD_PAN_SCAN)
x_offset = -(ddvd_screeninfo_xres * 16 / 15 - ddvd_screeninfo_xres) / 2;
return x_offset;
}
static int calc_y_scale_offset(int dvd_aspect, int tv_mode, int tv_mode2, int tv_aspect)
{
int y_offset = 0;
if (dvd_aspect == 0 && tv_mode == DDVD_LETTERBOX) {
switch (tv_aspect) {
case DDVD_16_10:
y_offset = (ddvd_screeninfo_yres * 15 / 12 - ddvd_screeninfo_yres) / 2; // correct 16:10 (broacom 15:9) letterbox overlay
break;
case DDVD_16_9:
y_offset = (ddvd_screeninfo_yres * 4 / 3 - ddvd_screeninfo_yres) / 2; // correct 16:9 letterbox overlay
default:
break;
}
}
if (dvd_aspect >= 2 && tv_aspect == DDVD_4_3 && tv_mode == DDVD_LETTERBOX)
y_offset = -(ddvd_screeninfo_yres - ddvd_screeninfo_yres * 3 / 4) / 2;
if (dvd_aspect >= 2 && tv_aspect == DDVD_16_10 && tv_mode2 == DDVD_LETTERBOX)
y_offset = -(ddvd_screeninfo_yres - ddvd_screeninfo_yres * 15 / 16) / 2;
return y_offset;
}
#if CONFIG_API_VERSION >= 3
static int readMpegProc(char *str, int decoder)
{
int val = -1;
char tmp[64];
sprintf(tmp, "/proc/stb/vmpeg/%d/%s", decoder, str);
FILE *f = fopen(tmp, "r");
if (f) {
fscanf(f, "%x", &val);
fclose(f);
}
return val;
}
static int readApiSize(int fd, int *xres, int *yres, int *aspect)
{
video_size_t size;
if (!ioctl(fd, VIDEO_GET_SIZE, &size)) {
*xres = size.w;
*yres = size.h;
*aspect = size.aspect_ratio == 0 ? 2 : 3; // convert dvb api to etsi
return 0;
}
return -1;
}
static int readApiFrameRate(int fd, int *framerate)
{
unsigned int frate;
if (!ioctl(fd, VIDEO_GET_FRAME_RATE, &frate)) {
*framerate = frate;
return 0;
}
return -1;
}
#endif
// the main player loop
enum ddvd_result ddvd_run(struct ddvd *playerconfig)
{
{ char * DL = getenv("LIBDVD_DEBUG");
if (DL)
DebugLevel = atoi(DL);
}
if (playerconfig->lfb == NULL) {
Debug(1, "Frame/backbuffer not given to libdreamdvd. Will not start the player !\n");
return DDVD_INVAL;
}
// we need to know the first vts_change and the next cell change for resuming a dvd
int first_vts_change = 1;
int next_cell_change = 0;
int ddvd_have_ntsc = -1;
ddvd_screeninfo_xres = playerconfig->xres;
ddvd_screeninfo_yres = playerconfig->yres;
ddvd_screeninfo_stride = playerconfig->stride;
int ddvd_screeninfo_bypp = playerconfig->bypp;
int message_pipe = playerconfig->message_pipe[1];
int key_pipe = playerconfig->key_pipe[0];
unsigned char *p_lfb = playerconfig->lfb;
enum ddvd_result res = DDVD_OK;
int msg;
// try to load liba52.so.0 for softdecoding
int have_liba52 = ddvd_load_liba52();
int audio_lock = 0;
int spu_lock = 0;
unsigned long long vpts = 0, apts = 0, spts = 0, pts = 0;
unsigned long long steppts = 0; // target pts for STEP mode
ddvd_lbb_changed = 0;
ddvd_clear_screen = 0;
int have_highlight = 0;
int ddvd_wait_highlight = 0;
const char *dvd_titlestring = NULL;
playerconfig->in_menu = 0;
int ddvd_spu_ind = 0;
int ddvd_spu_play = 0;
// decide which resize routine we should use
// on 4bpp mode we use bicubic resize for sd skins because we get much better results with subtitles and the speed is ok
// for hd skins we use nearest neighbor resize because upscaling to hd is too slow with bicubic resize
// for bypp != 0 resize function is set in spu/highlight code
if (ddvd_screeninfo_bypp == 1)
ddvd_resize_pixmap = &ddvd_resize_pixmap_1bpp;
uint8_t *last_iframe = NULL;
// init backbuffer (SPU)
ddvd_lbb = malloc(720 * 576); // the spu backbuffer is always max DVD PAL 720x576 pixel (NTSC 720x480)
if (ddvd_lbb == NULL) {
Perror("SPU decode buffer <mem allocation failed>");
res = DDVD_NOMEM;
goto err_malloc;
}
ddvd_lbb2 = malloc(ddvd_screeninfo_xres * ddvd_screeninfo_yres * ddvd_screeninfo_bypp);
if (ddvd_lbb2 == NULL) {
Perror("SPU to screen buffer <mem allocation failed>");
res = DDVD_NOMEM;
goto err_malloc;
}
#define SPU_BUFLEN (2 * (128 * 1024))
unsigned long long spu_backpts[NUM_SPU_BACKBUFFER];
unsigned char *ddvd_spu[NUM_SPU_BACKBUFFER];
pci_t *ddvd_pci[NUM_SPU_BACKBUFFER];
{ int i;
for (i = 0; i < NUM_SPU_BACKBUFFER; i++) {
ddvd_spu[i] = malloc(SPU_BUFLEN); // buffers for decoded SPU packets
if (ddvd_spu[i] == NULL) {
Perror("SPU backbuffer <mem allocation failed>");
res = DDVD_NOMEM;
goto err_malloc;
}
ddvd_pci[i] = malloc(sizeof(pci_t)); // buffers for pci packets
if (ddvd_pci[i] == NULL) {
Perror("PCI backbuffer <mem allocation failed>");
res = DDVD_NOMEM;
goto err_malloc;
}
}
}
last_iframe = malloc(320 * 1024);
if (last_iframe == NULL) {
Perror("malloc last_iframe");
res = DDVD_NOMEM;
goto err_malloc;
}
struct ddvd_resize_return blit_area;
memset(p_lfb, 0, ddvd_screeninfo_stride * ddvd_screeninfo_yres); //clear screen
blit_area.x_start = blit_area.y_start = 0;
blit_area.x_end = ddvd_screeninfo_xres - 1;
blit_area.y_end = ddvd_screeninfo_yres - 1;
blit_area.x_offset = 0;
blit_area.y_offset = 0;
blit_area.width = ddvd_screeninfo_xres;
blit_area.height = ddvd_screeninfo_yres;
msg = DDVD_SCREEN_UPDATE;
safe_write(message_pipe, &msg, sizeof(int));
safe_write(message_pipe, &blit_area, sizeof(struct ddvd_resize_return));
Debug(1, "Opening output...\n");
#if CONFIG_API_VERSION == 1
ddvd_output_fd = open("/dev/video", O_WRONLY);
if (ddvd_output_fd == -1) {
Perror("/dev/video");
res = DDVD_BUSY;
goto err_open_output_fd;
}
ddvd_fdvideo = open("/dev/dvb/card0/video0", O_RDWR);
if (ddvd_fdvideo == -1) {
Perror("/dev/dvb/card0/video0");
res = DDVD_BUSY;
goto err_open_fdvideo;
}
ddvd_fdaudio = open("/dev/dvb/card0/audio0", O_RDWR);
if (ddvd_fdaudio == -1) {
Perror("/dev/dvb/card0/audio0");
res = DDVD_BUSY;
goto err_open_fdaudio;
}
ddvd_ac3_fd = open("/dev/sound/dsp1", O_RDWR);
if (ddvd_ac3_fd == -1) {
Perror("/dev/sound/dsp1");
res = DDVD_BUSY;
goto err_open_ac3_fd;
}
if (ioctl(ddvd_fdvideo, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY) < 0)
Perror("VIDEO_SELECT_SOURCE");
if (ioctl(ddvd_fdvideo, VIDEO_CLEAR_BUFFER) < 0)
Perror("VIDEO_CLEAR_BUFFER");
if (ioctl(ddvd_fdvideo, VIDEO_PLAY) < 0)
Perror("VIDEO_PLAY");
if (ioctl(ddvd_fdaudio, AUDIO_SELECT_SOURCE, AUDIO_SOURCE_MEMORY) < 0)
Perror("AUDIO_SELECT_SOURCE");
if (ioctl(ddvd_fdaudio, AUDIO_CLEAR_BUFFER) < 0)
Perror("AUDIO_CLEAR_BUFFER");
if (ioctl(ddvd_fdaudio, AUDIO_PLAY) < 0)
Perror("AUDIO_PLAY");
#elif CONFIG_API_VERSION == 3
ddvd_output_fd = ddvd_fdvideo = open("/dev/dvb/adapter0/video0", O_RDWR);
if (ddvd_fdvideo == -1) {
Perror("/dev/dvb/adapter0/video0");
res = DDVD_BUSY;
goto err_open_fdvideo;
}
ddvd_ac3_fd = ddvd_fdaudio = open("/dev/dvb/adapter0/audio0", O_RDWR);
if (ddvd_fdaudio == -1) {
Perror("/dev/dvb/adapter0/audio0");
res = DDVD_BUSY;
goto err_open_ac3_fd;
}
if (ioctl(ddvd_fdvideo, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY) < 0)
Perror("VIDEO_SELECT_SOURCE");
if (ioctl(ddvd_fdvideo, VIDEO_CLEAR_BUFFER) < 0)
Perror("VIDEO_CLEAR_BUFFER");
if (ioctl(ddvd_fdvideo, VIDEO_SET_STREAMTYPE, 0) < 0) // set mpeg2
Perror("VIDEO_SET_STREAMTYPE");
if (ioctl(ddvd_fdvideo, VIDEO_PLAY) < 0)
Perror("VIDEO_PLAY");
if (ioctl(ddvd_fdaudio, AUDIO_SELECT_SOURCE, AUDIO_SOURCE_MEMORY) < 0)
Perror("AUDIO_SELECT_SOURCE");
if (ioctl(ddvd_fdaudio, AUDIO_CLEAR_BUFFER) < 0)
Perror("AUDIO_CLEAR_BUFFER");
if (ioctl(ddvd_fdaudio, AUDIO_PLAY) < 0)
Perror("AUDIO_PLAY");
#else
# error please define CONFIG_API_VERSION to be 1 or 3
#endif
int i;
// show startup screen
#if SHOW_START_SCREEN == 1
# if CONFIG_API_VERSION == 1
//that really sucks but there is no other way
for (i = 0; i < 10; i++)
safe_write(ddvd_output_fd, ddvd_startup_logo, sizeof(ddvd_startup_logo));
# else
unsigned char pes_header[] = { 0x00, 0x00, 0x01, 0xE0, 0x00, 0x00, 0x80, 0x00, 0x00 };
safe_write(ddvd_output_fd, pes_header, sizeof(pes_header));
safe_write(ddvd_output_fd, ddvd_startup_logo, sizeof(ddvd_startup_logo));
# endif
#endif
int audio_type = DDVD_UNKNOWN;
#if CONFIG_API_VERSION == 3
ddvd_set_pcr_offset();
#endif
uint8_t mem[DVD_VIDEO_LB_LEN];
uint8_t *buf = mem;
int result, event, len;
unsigned char lpcm_data[2048 * 6 * 6 /*4608 */ ];
int mpa_header_length;
int mpa_count, mpa_count2;
uint8_t mpa_data[2048 * 4];
int ac3_len;
int16_t ac3_tmp[2048 * 6 * 6];
ddvd_mpa_init(48000, 192000); //init MPA Encoder with 48kHz and 192k Bitrate
int ac3thru = 1;
if (have_liba52) {
state = a52_init(0); //init AC3 Decoder
ac3thru = playerconfig->ac3thru;
}
char osdtext[512];
osdtext[0] = 0;
int tv_aspect = playerconfig->aspect; // 0-> 4:3 lb 1-> 4:3 ps 2-> 16:9 3-> always 16:9
int tv_mode = playerconfig->tv_mode;
int tv_mode2 = playerconfig->tv_mode2; // just used when tv_aspect is 16:10 and dvd_aspect is 16:9
int dvd_aspect = 0; // 0-> 4:3 2-> 16:9
int dvd_scale_perm = 0;
int tv_scale = 0; // 0-> off 1-> letterbox 2-> panscan
int spu_active_id = -1;
int spu_index = -1;
int finished = 0;
int audio_id;
int report_audio_info = 0;
struct ddvd_spu_return last_spu_return;
struct ddvd_spu_return cur_spu_return;
struct ddvd_resize_return last_blit_area;
memcpy(&last_blit_area, &blit_area, sizeof(struct ddvd_resize_return));
last_spu_return.x_start = last_spu_return.y_start = 0;
last_spu_return.x_end = last_spu_return.y_end = 0;
ddvd_trickmode = TOFF;
ddvd_trickspeed = 0;
int rccode;
int ismute = 0;
if (ioctl(ddvd_fdaudio, AUDIO_SET_AV_SYNC, 1) < 0)
Perror("AUDIO_SET_AV_SYNC");
#if CONFIG_API_VERSION == 1
// set video system
int pal_ntsc = playerconfig->tv_system;
int saa;
if (pal_ntsc == 1)
saa = SAA_NTSC;
else
saa = SAA_PAL;
int saafd = open("/dev/dbox/saa0", O_RDWR);
if (saafd >= 0) {
if (ioctl(saafd, SAAIOSENC, &saa) < 0)
Perror("SAAIOSENC");
close(saafd);
}
#else
{
struct ddvd_size_evt s_evt;
struct ddvd_framerate_evt f_evt;
struct ddvd_progressive_evt p_evt;
int msg = DDVD_SIZE_CHANGED;
readApiSize(ddvd_fdvideo, &s_evt.width, &s_evt.height, &s_evt.aspect);
safe_write(message_pipe, &msg, sizeof(int));
safe_write(message_pipe, &s_evt, sizeof(s_evt));
msg = DDVD_FRAMERATE_CHANGED;
readApiFrameRate(ddvd_fdvideo, &f_evt.framerate);
safe_write(message_pipe, &msg, sizeof(int));
safe_write(message_pipe, &f_evt, sizeof(f_evt));
msg = DDVD_PROGRESSIVE_CHANGED;
p_evt.progressive = readMpegProc("progressive", 0);
safe_write(message_pipe, &msg, sizeof(int));
safe_write(message_pipe, &p_evt, sizeof(p_evt));
}
#endif
/* open dvdnav handle */
Debug(1, "Opening DVD...%s\n", playerconfig->dvd_path);
if (dvdnav_open(&dvdnav, playerconfig->dvd_path) != DVDNAV_STATUS_OK) {
Debug(1, "Error on dvdnav_open\n");
sprintf(osdtext, "Error: Cant open DVD Source: %s", playerconfig->dvd_path);
msg = DDVD_SHOWOSD_STRING;
safe_write(message_pipe, &msg, sizeof(int));
safe_write(message_pipe, &osdtext, sizeof(osdtext));
res = DDVD_FAIL_OPEN;
goto err_dvdnav_open;
}
/* set read ahead cache usage to no */
if (dvdnav_set_readahead_flag(dvdnav, 0) != DVDNAV_STATUS_OK) {
Debug(1, "Error on dvdnav_set_readahead_flag: %s\n", dvdnav_err_to_string(dvdnav));
res = DDVD_FAIL_PREFS;
goto err_dvdnav;
}
/* set the language */
if (dvdnav_menu_language_select(dvdnav, playerconfig->language) != DVDNAV_STATUS_OK ||
dvdnav_audio_language_select(dvdnav, playerconfig->language) != DVDNAV_STATUS_OK ||
dvdnav_spu_language_select(dvdnav, playerconfig->language) != DVDNAV_STATUS_OK) {
Debug(1, "Error on setting languages: %s\n", dvdnav_err_to_string(dvdnav));
res = DDVD_FAIL_PREFS;
goto err_dvdnav;
}
/* set the PGC positioning flag to have position information relatively to the
* whole feature instead of just relatively to the current chapter */
if (dvdnav_set_PGC_positioning_flag(dvdnav, 1) != DVDNAV_STATUS_OK) {
Debug(1, "Error on dvdnav_set_PGC_positioning_flag: %s\n", dvdnav_err_to_string(dvdnav));
res = DDVD_FAIL_PREFS;
goto err_dvdnav;
}
audio_id = dvdnav_get_active_audio_stream(dvdnav);
ddvd_playmode = PLAY;
dvdnav_highlight_event_t highlight_event;
ddvd_play_empty(FALSE);
ddvd_get_time(); //set timestamp
if (dvdnav_get_title_string(dvdnav, &dvd_titlestring) == DVDNAV_STATUS_OK)
strncpy(playerconfig->title_string, dvd_titlestring, 96);
if (strlen(playerconfig->title_string) == 0) {
// DVD has no title set,, use dvd_path info
char *sl = strrchr(playerconfig->dvd_path, '/');
if (sl == NULL)
sl = playerconfig->dvd_path;
else
sl++;
strncpy(playerconfig->title_string, sl, 96);
sl = playerconfig->title_string;
int len = strlen(sl);
if (sl[len - 4] == '.' && sl[len - 3] == 'i' && sl[len - 2] == 's' && sl[len - 1] == '0')
sl[len - 4] = '\0';
}
Debug(1, "DVD Title: %s (DVD says: %s)\n", playerconfig->title_string, dvd_titlestring);
msg = DDVD_SHOWOSD_TITLESTRING;
safe_write(message_pipe, &msg, sizeof(int));
if( dvdnav_title_play(dvdnav, 1 ) != DVDNAV_STATUS_OK)
Debug(1, "cannot set title (can't decrypt DVD?)\n");
if( dvdnav_menu_call(dvdnav, DVD_MENU_Title ) != DVDNAV_STATUS_OK) {
/* Try going to menu root */
if( dvdnav_menu_call(dvdnav, DVD_MENU_Root) != DVDNAV_STATUS_OK)
Debug(1, "cannot go to dvd menu\n");
}
/* the read loop which regularly calls dvdnav_get_next_block
* and handles the returned events */
int reached_eof = 0;
int reached_sof = 0;
uint64_t now;
int in_menu = 0;
pci_t *pci = NULL;
dvdnav_still_event_t still_event;
int have_still_event = 0;
while (!finished) {
dsi_t *dsi = 0;
int draw_osd = 0;
/* the main reading function */
now = ddvd_get_time();
if (ddvd_playmode & (PLAY|STEP)) { // Skip when not in play/step mode
// trickmode
if (ddvd_trickmode & (TRICKFW | TRICKBW) && now >= ddvd_trick_timer_end) {
uint32_t pos, len;
dvdnav_get_position(dvdnav, &pos, &len);
if (!len)
len = 1;
// Backward: 90000 = 1 Sek. -> 45000 = 0.5 Sek. -> Speed Faktor=2
// Forward: 90000 = 1 Sek. -> 22500 = 0.25 Sek. -> Speed Faktor=2
#define FORWARD_WAIT 300
#define BACKWARD_WAIT 500
int64_t offset = (ddvd_trickspeed - 1) * 90000L * (ddvd_trickmode & TRICKBW ? BACKWARD_WAIT : FORWARD_WAIT) / 1000;
int64_t newpos = (int64_t)pos +(offset + (int64_t)(vpts > pts ? pts - vpts : 0)) * (int64_t)len / ddvd_lastCellEventInfo.pgc_length;
Debug(1, "FAST FW/BW: %d -> %lld - %lld - SPU clr=%d->%d vpts=%llu pts=%llu\n", pos, newpos, offset, ddvd_spu_play, ddvd_spu_ind, vpts, pts);