forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blkdev_cdimage.cpp
2339 lines (2149 loc) · 60.2 KB
/
blkdev_cdimage.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
/*
* UAE
*
* CD image file support
*
* - iso (2048/2352 block size)
* - cue/bin, cue/bin/wav, cue/bin/mp3, cue/bin/flac
* - ccd/img and ccd/img/sub
* - chd cd
*
* Copyright 2010-2013 Toni Wilen
*
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include <sys/timeb.h>
#include "options.h"
#include "traps.h"
#include "blkdev.h"
#include "zfile.h"
#include "gui.h"
#include "fsdb.h"
#include "threaddep/thread.h"
#include "scsidev.h"
#include "mp3decoder.h"
#include "cda_play.h"
#include "memory.h"
#include "audio.h"
#include "uae.h"
#include "uae/cdrom.h"
#ifdef RETROPLATFORM
#include "rp.h"
#endif
#define FLAC__NO_DLL
#include "FLAC/stream_decoder.h"
#ifdef WITH_CHD
#include "archivers/chd/chdtypes.h"
#include "archivers/chd/chd.h"
#include "archivers/chd/chdcd.h"
#endif
#define scsi_log write_log
#define CDDA_BUFFERS 12
enum audenc { AUDENC_NONE, AUDENC_PCM, AUDENC_MP3, AUDENC_FLAC, ENC_CHD };
struct cdtoc
{
struct zfile *handle;
uae_s64 offset;
uae_u8 *data;
struct zfile *subhandle;
int suboffset;
uae_u8 *subdata;
uae_s64 filesize;
TCHAR *fname;
TCHAR *extrainfo;
int address;
uae_u8 adr, ctrl;
int track;
int size;
int skipsize; // bytes to skip after each block
int index1; // distance between index0 and index1
int pregap; // sectors of silence
int postgap; // sectors of silence
audenc enctype;
int writeoffset;
int subcode;
#ifdef WITH_CHD
const cdrom_track_info *chdtrack;
#endif
};
struct cdunit {
bool enabled;
bool open;
uae_u8 buffer[2352];
struct cdtoc toc[102];
int tracks;
uae_u64 cdsize;
int blocksize;
int cdda_play_state;
int cdda_play;
int cdda_paused;
int cdda_volume[2];
int cdda_scan;
int cd_last_pos;
int cdda_start, cdda_end;
play_subchannel_callback cdda_subfunc;
play_status_callback cdda_statusfunc;
int cdda_delay, cdda_delay_frames;
bool thread_active;
TCHAR imgname_in[MAX_DPATH];
TCHAR imgname_out[MAX_DPATH];
uae_sem_t sub_sem;
struct device_info di;
#ifdef WITH_CHD
chd_file *chd_f;
cdrom_file *chd_cdf;
#endif
volatile int cda_bufon[2];
cda_audio *cda;
struct cd_audio_state cas;
};
static struct cdunit cdunits[MAX_TOTAL_SCSI_DEVICES];
static int bus_open;
static volatile int cdimage_unpack_thread, cdimage_unpack_active;
static smp_comm_pipe unpack_pipe;
static uae_sem_t play_sem;
static struct cdunit *unitisopen (int unitnum)
{
struct cdunit *cdu = &cdunits[unitnum];
if (cdu->open)
return cdu;
return NULL;
}
static struct cdtoc *findtoc (struct cdunit *cdu, int *sectorp, bool data)
{
int i;
int sector;
if (*sectorp < 0)
return NULL;
sector = *sectorp;
for (i = 0; i <= cdu->tracks; i++) {
struct cdtoc *t = &cdu->toc[i];
if (t->address - t->index1 > sector) {
if (i == 0) {
*sectorp = 0;
return t;
}
t--;
sector -= t->address - t->index1;
if (!data && sector < t->pregap)
return NULL; // pregap silence
*sectorp = sector;
return t;
}
}
return NULL;
}
static int do_read (struct cdunit *cdu, struct cdtoc *t, uae_u8 *data, int sector, int offset, int size, bool audio)
{
if (t->enctype == ENC_CHD) {
#ifdef WITH_CHD
int type = CD_TRACK_MODE1_RAW;
uae_u8 tmpbuf[2352];
if (size > 2352)
return 0;
switch (size)
{
case 2352:
type = CD_TRACK_MODE1_RAW;
offset = 0;
break;
case 2336:
type = CD_TRACK_MODE2;
offset = 0;
break;
case 2048:
type = CD_TRACK_MODE1;
offset = 0;
break;
}
if (audio && size == 2352)
type = CD_TRACK_AUDIO;
if (cdrom_read_data(cdu->chd_cdf, sector + t->offset, tmpbuf, type, true)) {
memcpy(data, tmpbuf + offset, size);
return 1;
}
return 0;
#endif
} else if (t->handle) {
int ssize = t->size + t->skipsize;
zfile_fseek (t->handle, t->offset + (uae_u64)sector * ssize + offset, SEEK_SET);
return zfile_fread (data, 1, size, t->handle) == size;
}
return 0;
}
// WOHOO, library that supports virtual file access functions. Perfect!
static void flac_metadata_callback (const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
struct cdtoc *t = (struct cdtoc*)client_data;
if (t->data)
return;
if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
t->filesize = metadata->data.stream_info.total_samples * (metadata->data.stream_info.bits_per_sample / 8) * metadata->data.stream_info.channels;
}
}
static void flac_error_callback (const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
return;
}
static FLAC__StreamDecoderWriteStatus flac_write_callback (const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
struct cdtoc *t = (struct cdtoc*)client_data;
uae_u16 *p = (uae_u16*)(t->data + t->writeoffset);
int size = 4;
for (int i = 0; i < frame->header.blocksize && t->writeoffset < t->filesize - size; i++, t->writeoffset += size) {
*p++ = (FLAC__int16)buffer[0][i];
*p++ = (FLAC__int16)buffer[1][i];
}
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
static FLAC__StreamDecoderReadStatus file_read_callback (const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
{
struct cdtoc *t = (struct cdtoc*)client_data;
if (zfile_ftell (t->handle) >= zfile_size (t->handle))
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
return zfile_fread (buffer, *bytes, 1, t->handle) ? FLAC__STREAM_DECODER_READ_STATUS_CONTINUE : FLAC__STREAM_DECODER_READ_STATUS_ABORT;
}
static FLAC__StreamDecoderSeekStatus file_seek_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
{
struct cdtoc *t = (struct cdtoc*)client_data;
zfile_fseek (t->handle, absolute_byte_offset, SEEK_SET);
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
}
static FLAC__StreamDecoderTellStatus file_tell_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
{
struct cdtoc *t = (struct cdtoc*)client_data;
*absolute_byte_offset = zfile_ftell (t->handle);
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}
static FLAC__StreamDecoderLengthStatus file_len_callback (const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
{
struct cdtoc *t = (struct cdtoc*)client_data;
*stream_length = zfile_size (t->handle);
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}
static FLAC__bool file_eof_callback (const FLAC__StreamDecoder *decoder, void *client_data)
{
struct cdtoc *t = (struct cdtoc*)client_data;
return zfile_ftell (t->handle) >= zfile_size (t->handle);
}
static void flac_get_size (struct cdtoc *t)
{
FLAC__StreamDecoder *decoder = FLAC__stream_decoder_new ();
if (decoder) {
FLAC__stream_decoder_set_md5_checking (decoder, false);
int init_status = FLAC__stream_decoder_init_stream (decoder,
&file_read_callback, &file_seek_callback, &file_tell_callback,
&file_len_callback, &file_eof_callback,
&flac_write_callback, &flac_metadata_callback, &flac_error_callback, t);
FLAC__stream_decoder_process_until_end_of_metadata (decoder);
FLAC__stream_decoder_delete (decoder);
}
}
static uae_u8 *flac_get_data (struct cdtoc *t)
{
write_log (_T("FLAC: unpacking '%s'..\n"), zfile_getname (t->handle));
t->writeoffset = 0;
FLAC__StreamDecoder *decoder = FLAC__stream_decoder_new ();
if (decoder) {
FLAC__stream_decoder_set_md5_checking (decoder, false);
int init_status = FLAC__stream_decoder_init_stream (decoder,
&file_read_callback, &file_seek_callback, &file_tell_callback,
&file_len_callback, &file_eof_callback,
&flac_write_callback, &flac_metadata_callback, &flac_error_callback, t);
FLAC__stream_decoder_process_until_end_of_stream (decoder);
FLAC__stream_decoder_delete (decoder);
write_log (_T("FLAC: %s unpacked\n"), zfile_getname (t->handle));
}
return t->data;
}
void sub_to_interleaved (const uae_u8 *s, uae_u8 *d)
{
for (int i = 0; i < 8 * SUB_ENTRY_SIZE; i ++) {
int dmask = 0x80;
int smask = 1 << (7 - (i & 7));
(*d) = 0;
for (int j = 0; j < 8; j++) {
(*d) |= (s[(i / 8) + j * SUB_ENTRY_SIZE] & smask) ? dmask : 0;
dmask >>= 1;
}
d++;
}
}
void sub_to_deinterleaved (const uae_u8 *s, uae_u8 *d)
{
for (int i = 0; i < 8 * SUB_ENTRY_SIZE; i ++) {
int dmask = 0x80;
int smask = 1 << (7 - (i / SUB_ENTRY_SIZE));
(*d) = 0;
for (int j = 0; j < 8; j++) {
(*d) |= (s[(i % SUB_ENTRY_SIZE) * 8 + j] & smask) ? dmask : 0;
dmask >>= 1;
}
d++;
}
}
static int getsub_deinterleaved (uae_u8 *dst, struct cdunit *cdu, struct cdtoc *t, int sector)
{
int ret = 0;
uae_sem_wait (&cdu->sub_sem);
if (t->subcode) {
if (t->enctype == ENC_CHD) {
#ifdef WITH_CHD
const cdrom_track_info *cti = t->chdtrack;
if (cdrom_read_subcode(cdu->chd_cdf, sector, dst, false))
ret = t->subcode;
#endif
} else if (t->subhandle) {
int offset = 0;
int totalsize = SUB_CHANNEL_SIZE;
if (t->skipsize) {
totalsize += t->size;
offset = t->size;
}
zfile_fseek (t->subhandle, (uae_u64)sector * totalsize + t->suboffset + offset, SEEK_SET);
if (zfile_fread (dst, SUB_CHANNEL_SIZE, 1, t->subhandle) > 0)
ret = t->subcode;
} else {
memcpy (dst, t->subdata + sector * SUB_CHANNEL_SIZE + t->suboffset, SUB_CHANNEL_SIZE);
ret = t->subcode;
}
}
if (!ret) {
memset (dst, 0, SUB_CHANNEL_SIZE);
// regenerate Q-subchannel
uae_u8 *s = dst + SUB_ENTRY_SIZE;
s[0] = (t->ctrl << 4) | (t->adr << 0);
s[1] = tobcd (t - &cdu->toc[0] + 1);
s[2] = tobcd (1);
int msf = lsn2msf (sector);
tolongbcd (s + 7, msf);
msf = lsn2msf (sector - t->address - 150);
tolongbcd (s + 3, msf);
ret = 2;
}
if (ret == 1) {
uae_u8 tmp[SUB_CHANNEL_SIZE];
memcpy (tmp, dst, SUB_CHANNEL_SIZE);
sub_to_deinterleaved (tmp, dst);
ret = 2;
}
#if 0
uae_u8 *s = dst + SUB_ENTRY_SIZE;
write_log(_T("CTRLADR:%02X TRK=%02X IDX=%02X MSF=%02X:%02X:%02X %02X:%02X:%02X\n"),
s[0], s[1], s[2],
s[3], s[4], s[5],
s[7], s[8], s[9]);
#endif
uae_sem_post (&cdu->sub_sem);
return ret;
}
static void dosub (struct cdunit *cdu, uae_u8 *subbuf)
{
uae_u8 subbuf2[SUB_CHANNEL_SIZE];
if (!cdu->cdda_subfunc)
return;
if (!subbuf) {
memset (subbuf2, 0, sizeof subbuf2);
cdu->cdda_subfunc (subbuf2, 1);
return;
}
sub_to_interleaved (subbuf, subbuf2);
cdu->cdda_subfunc (subbuf2, 1);
}
static int setstate (struct cdunit *cdu, int state, int playpos)
{
cdu->cdda_play_state = state;
if (cdu->cdda_statusfunc)
return cdu->cdda_statusfunc (cdu->cdda_play_state, playpos);
return 0;
}
static void *cdda_unpack_func (void *v)
{
cdimage_unpack_thread = 1;
mp3decoder *mp3dec = NULL;
for (;;) {
uae_u32 cduidx = read_comm_pipe_u32_blocking (&unpack_pipe);
if (cdimage_unpack_thread == 0)
break;
uae_u32 tocidx = read_comm_pipe_u32_blocking (&unpack_pipe);
struct cdunit *cdu = &cdunits[cduidx];
struct cdtoc *t = &cdu->toc[tocidx];
if (t->handle) {
// force unpack if handle points to delayed zipped file
uae_s64 pos = zfile_ftell (t->handle);
zfile_fseek (t->handle, -1, SEEK_END);
uae_u8 b;
zfile_fread (&b, 1, 1, t->handle);
zfile_fseek (t->handle, pos, SEEK_SET);
if (!t->data && (t->enctype == AUDENC_MP3 || t->enctype == AUDENC_FLAC)) {
t->data = xcalloc (uae_u8, t->filesize + 2352);
cdimage_unpack_active = 1;
if (t->data) {
if (t->enctype == AUDENC_MP3) {
if (!mp3dec) {
try {
mp3dec = new mp3decoder();
} catch (exception) { };
}
if (mp3dec)
t->data = mp3dec->get (t->handle, t->data, t->filesize);
} else if (t->enctype == AUDENC_FLAC) {
flac_get_data (t);
}
}
}
}
cdimage_unpack_active = 2;
}
delete mp3dec;
cdimage_unpack_thread = -1;
return 0;
}
static void audio_unpack (struct cdunit *cdu, struct cdtoc *t)
{
// do this even if audio is not compressed, t->handle also could be
// compressed and we want to unpack it in background too
while (cdimage_unpack_active == 1)
sleep_millis(10);
cdimage_unpack_active = 0;
write_comm_pipe_u32 (&unpack_pipe, cdu - &cdunits[0], 0);
write_comm_pipe_u32 (&unpack_pipe, t - &cdu->toc[0], 1);
while (cdimage_unpack_active == 0)
sleep_millis(10);
}
static void next_cd_audio_buffer_callback(int bufnum, void *params)
{
struct cdunit *cdu = (struct cdunit*)params;
uae_sem_wait(&play_sem);
if (bufnum >= 0) {
cdu->cda_bufon[bufnum] = 0;
bufnum = 1 - bufnum;
if (cdu->cda_bufon[bufnum])
audio_cda_new_buffer(&cdu->cas, (uae_s16*)cdu->cda->buffers[bufnum], CDDA_BUFFERS * 2352 / 4, bufnum, next_cd_audio_buffer_callback, cdu);
else
bufnum = -1;
}
if (bufnum < 0) {
audio_cda_new_buffer(&cdu->cas, NULL, -1, 0, NULL, cdu);
}
uae_sem_post(&play_sem);
}
static bool cdda_play_func2 (struct cdunit *cdu, int *outpos)
{
int cdda_pos = cdu->cdda_start;
int bufnum;
int oldplay;
int idleframes = 0;
int silentframes = 0;
bool foundsub;
int oldtrack = -1;
int mode = currprefs.sound_cdaudio;
bool restart = false;
bool first = true;
cdu->thread_active = true;
memset(&cdu->cas, 0, sizeof(struct cd_audio_state));
while (cdu->cdda_play == 0)
sleep_millis(10);
oldplay = -1;
cdu->cda_bufon[0] = cdu->cda_bufon[1] = 0;
bufnum = 0;
cdu->cda = new cda_audio (CDDA_BUFFERS, 2352, 44100, mode != 0);
while (cdu->cdda_play > 0) {
if (oldplay != cdu->cdda_play) {
struct cdtoc *t;
int sector, diff;
struct _timeb tb1, tb2;
idleframes = 0;
silentframes = 0;
foundsub = false;
_ftime (&tb1);
cdda_pos = cdu->cdda_start;
oldplay = cdu->cdda_play;
sector = cdu->cd_last_pos = cdda_pos;
t = findtoc (cdu, §or, false);
if (!t) {
sector = cdu->cd_last_pos = cdda_pos + 2 * 75;
t = findtoc (cdu, §or, false);
if (!t) {
write_log (_T("IMAGE CDDA: illegal sector number %d\n"), cdu->cdda_start);
setstate (cdu, AUDIO_STATUS_PLAY_ERROR, -1);
} else {
audio_unpack (cdu, t);
}
} else {
write_log (_T("IMAGE CDDA: playing from %d to %d, track %d ('%s', offset %lld, secoffset %d (%d))\n"),
cdu->cdda_start, cdu->cdda_end, t->track, t->fname, t->offset, sector, t->index1);
oldtrack = t->track;
audio_unpack (cdu, t);
}
idleframes = cdu->cdda_delay_frames;
while (cdu->cdda_paused && cdu->cdda_play > 0) {
sleep_millis(10);
idleframes = -1;
}
if (cdu->cdda_scan == 0) {
// find possible P-subchannel=1 and fudge starting point so that
// buggy CD32/CDTV software CD+G handling does not miss any frames
bool seenindex = false;
for (sector = cdda_pos - 200; sector < cdda_pos; sector++) {
int sec = sector;
t = findtoc (cdu, &sec, false);
if (t) {
uae_u8 subbuf[SUB_CHANNEL_SIZE];
getsub_deinterleaved (subbuf, cdu, t, sector);
if (seenindex) {
for (int i = 2 * SUB_ENTRY_SIZE; i < SUB_CHANNEL_SIZE; i++) {
if (subbuf[i]) { // non-zero R-W subchannels
int diff = cdda_pos - sector + 2;
write_log (_T("-> CD+G start pos fudge -> %d (%d)\n"), sector, -diff);
idleframes -= diff;
cdda_pos = sector;
break;
}
}
} else if (subbuf[0] == 0xff) { // P == 1?
seenindex = true;
}
}
}
}
cdda_pos -= idleframes;
if (*outpos < 0) {
_ftime (&tb2);
diff = (tb2.time * (uae_s64)1000 + tb2.millitm) - (tb1.time * (uae_s64)1000 + tb1.millitm);
diff -= cdu->cdda_delay;
if (idleframes >= 0 && diff < 0 && cdu->cdda_play > 0)
sleep_millis(-diff);
setstate (cdu, AUDIO_STATUS_IN_PROGRESS, cdda_pos);
}
sector = cdda_pos;
struct cdtoc *t1 = findtoc (cdu, §or, false);
int tsector = cdda_pos + 2 * 75;
struct cdtoc *t2 = findtoc (cdu, &tsector, false);
if (t1 != t2) {
for (sector = cdda_pos; sector < cdda_pos + 2 * 75; sector++) {
int sec = sector;
t = findtoc (cdu, &sec, false);
if (t == t2)
break;
silentframes++;
}
}
}
if (mode) {
while (cdu->cda_bufon[bufnum] && cdu->cdda_play > 0) {
if (cd_audio_mode_changed) {
restart = true;
goto end;
}
sleep_millis(10);
}
} else {
cdu->cda->wait(bufnum);
}
cdu->cda_bufon[bufnum] = 0;
if (cdu->cdda_play <= 0)
goto end;
if (idleframes <= 0 && cdda_pos >= cdu->cdda_start && !isaudiotrack (&cdu->di.toc, cdda_pos)) {
setstate (cdu, AUDIO_STATUS_PLAY_ERROR, -1);
write_log (_T("IMAGE CDDA: attempted to play data track %d\n"), cdda_pos);
goto end; // data track?
}
if ((cdda_pos < cdu->cdda_end || cdu->cdda_end == 0xffffffff) && !cdu->cdda_paused && cdu->cdda_play > 0) {
struct cdtoc *t;
int sector, cnt;
int dofinish = 0;
gui_flicker_led (LED_CD, cdu->di.unitnum - 1, LED_CD_AUDIO);
setstate(cdu, AUDIO_STATUS_IN_PROGRESS, cdda_pos);
memset (cdu->cda->buffers[bufnum], 0, CDDA_BUFFERS * 2352);
for (cnt = 0; cnt < CDDA_BUFFERS && cdu->cdda_play > 0; cnt++) {
uae_u8 *dst = cdu->cda->buffers[bufnum] + cnt * 2352;
uae_u8 subbuf[SUB_CHANNEL_SIZE];
sector = cdda_pos;
memset (subbuf, 0, SUB_CHANNEL_SIZE);
t = findtoc (cdu, §or, false);
if (t) {
if (t->track != oldtrack) {
oldtrack = t->track;
write_log (_T("IMAGE CDDA: track %d ('%s', offset %lld, secoffset %d (%d))\n"),
t->track, t->fname, t->offset, sector, t->index1);
audio_unpack (cdu, t);
}
if (!(t->ctrl & 4)) {
if (t->enctype == ENC_CHD) {
#ifdef WITH_CHD
do_read (cdu, t, dst, sector, 0, t->size, true);
for (int i = 0; i < 2352; i+=2) {
uae_u8 p;
p = dst[i + 0];
dst[i + 0] = dst[i + 1];
dst[i +1] = p;
}
#endif
} else if (t->handle) {
int totalsize = t->size + t->skipsize;
int offset = t->offset;
if (offset >= 0) {
if ((t->enctype == AUDENC_MP3 || t->enctype == AUDENC_FLAC) && t->data) {
if (t->filesize >= sector * totalsize + offset + t->size)
memcpy (dst, t->data + sector * totalsize + offset, t->size);
} else if (t->enctype == AUDENC_PCM) {
if (sector * totalsize + offset + totalsize < t->filesize) {
zfile_fseek (t->handle, (uae_u64)sector * totalsize + offset, SEEK_SET);
zfile_fread (dst, t->size, 1, t->handle);
}
}
}
}
}
getsub_deinterleaved (subbuf, cdu, t, cdda_pos);
}
if (idleframes > 0 || silentframes > 0) {
if (idleframes > 0) {
idleframes--;
memset (subbuf, 0, SUB_CHANNEL_SIZE);
}
if (silentframes > 0)
silentframes--;
memset (dst, 0, 2352);
}
if (cdda_pos < cdu->cdda_start && cdu->cdda_scan == 0)
memset (dst, 0, 2352);
dosub (cdu, subbuf);
if (cdu->cdda_scan) {
cdda_pos += cdu->cdda_scan;
if (cdda_pos < 0)
cdda_pos = 0;
} else {
cdda_pos++;
}
if (cdda_pos - CDDA_BUFFERS < cdu->cdda_end && cdda_pos >= cdu->cdda_end)
dofinish = 1;
}
if (idleframes <= 0)
cdu->cd_last_pos = cdda_pos;
if (mode) {
if (cdu->cda_bufon[0] == 0 && cdu->cda_bufon[1] == 0) {
cdu->cda_bufon[bufnum] = 1;
next_cd_audio_buffer_callback(1 - bufnum, cdu);
}
audio_cda_volume(&cdu->cas, cdu->cdda_volume[0], cdu->cdda_volume[1]);
cdu->cda_bufon[bufnum] = 1;
} else {
cdu->cda_bufon[bufnum] = 1;
cdu->cda->setvolume (cdu->cdda_volume[0], cdu->cdda_volume[1]);
if (!cdu->cda->play (bufnum)) {
if (cdu->cdda_play > 0)
setstate (cdu, AUDIO_STATUS_PLAY_ERROR, -1);
goto end;
}
}
if (first) {
first = false;
setstate(cdu, -3, -1);
}
if (dofinish) {
cdda_pos = cdu->cdda_end + 1;
if (cdu->cdda_play >= 0)
setstate (cdu, AUDIO_STATUS_PLAY_COMPLETE, cdda_pos);
cdu->cdda_play = -1;
}
}
if (cdu->cda_bufon[0] == 0 && cdu->cda_bufon[1] == 0) {
while (cdu->cdda_paused && cdu->cdda_play == oldplay)
sleep_millis(10);
}
if (cd_audio_mode_changed) {
restart = true;
goto end;
}
bufnum = 1 - bufnum;
}
end:
*outpos = cdda_pos;
if (mode) {
next_cd_audio_buffer_callback(-1, cdu);
if (restart)
audio_cda_new_buffer(&cdu->cas, NULL, -1, -1, NULL, NULL);
} else {
cdu->cda->wait (0);
cdu->cda->wait (1);
}
while (cdimage_unpack_active == 1)
sleep_millis(10);
delete cdu->cda;
write_log (_T("IMAGE CDDA: thread killed (%s)\n"), restart ? _T("restart") : _T("play end"));
cd_audio_mode_changed = false;
return restart;
}
static void *cdda_play_func (void *v)
{
int outpos = -1;
struct cdunit *cdu = (struct cdunit*)v;
cd_audio_mode_changed = false;
for (;;) {
if (!cdda_play_func2(cdu, &outpos)) {
cdu->cdda_play = 0;
break;
}
cdu->cdda_start = outpos;
if (cdu->cdda_start + 150 >= cdu->cdda_end) {
if (cdu->cdda_play >= 0)
setstate (cdu, AUDIO_STATUS_PLAY_COMPLETE, cdu->cdda_end + 1);
cdu->cdda_play = -1;
break;
}
cdu->cdda_play = 1;
}
cdu->thread_active = false;
return NULL;
}
static void cdda_stop (struct cdunit *cdu)
{
if (cdu->cdda_play != 0) {
cdu->cdda_play = -1;
while (cdu->cdda_play && cdu->thread_active) {
sleep_millis(10);
}
cdu->cdda_play = 0;
}
cdu->cdda_paused = 0;
cdu->cdda_play_state = 0;
}
static int command_pause (int unitnum, int paused)
{
struct cdunit *cdu = unitisopen (unitnum);
if (!cdu)
return -1;
int old = cdu->cdda_paused;
if ((paused && cdu->cdda_play) || !paused)
cdu->cdda_paused = paused;
return old;
}
static int command_stop (int unitnum)
{
struct cdunit *cdu = unitisopen (unitnum);
if (!cdu)
return 0;
cdda_stop (cdu);
return 1;
}
static int command_play (int unitnum, int startlsn, int endlsn, int scan, play_status_callback statusfunc, play_subchannel_callback subfunc)
{
struct cdunit *cdu = unitisopen (unitnum);
if (!cdu)
return 0;
if (cdu->cdda_play) {
cdu->cdda_play = -1;
while (cdu->thread_active)
Sleep (10);
cdu->cdda_play = 0;
}
cdu->cd_last_pos = startlsn;
cdu->cdda_start = startlsn;
cdu->cdda_end = endlsn;
cdu->cdda_subfunc = subfunc;
cdu->cdda_statusfunc = statusfunc;
cdu->cdda_scan = scan > 0 ? 10 : (scan < 0 ? 10 : 0);
cdu->cdda_delay = setstate (cdu, -1, -1);
cdu->cdda_delay_frames = setstate (cdu, -2, -1);
setstate (cdu, cdu->cdda_delay > 0 || cdu->cdda_delay_frames ? AUDIO_STATUS_NOT_SUPPORTED : AUDIO_STATUS_IN_PROGRESS, -1);
if (!isaudiotrack (&cdu->di.toc, startlsn)) {
setstate (cdu, AUDIO_STATUS_PLAY_ERROR, -1);
return 0;
}
if (!cdu->thread_active) {
uae_start_thread (_T("cdimage_cdda_play"), cdda_play_func, cdu, NULL);
while (!cdu->thread_active)
Sleep (10);
}
cdu->cdda_play++;
return 1;
}
static int command_qcode (int unitnum, uae_u8 *buf, int sector, bool all)
{
struct cdunit *cdu = unitisopen (unitnum);
if (!cdu)
return 0;
uae_u8 subbuf[SUB_CHANNEL_SIZE];
uae_u8 *p;
int trk;
int pos;
int status;
memset (buf, 0, SUBQ_SIZE);
p = buf;
status = cdu->cdda_play_state;
if (cdu->cdda_play > 0 && cdu->cdda_paused)
status = AUDIO_STATUS_PAUSED;
if (sector < 0)
pos = cdu->cd_last_pos;
else
pos = sector;
p[1] = status;
p[3] = 12;
p = buf + 4;
struct cdtoc *td = NULL;
for (trk = 0; trk <= cdu->tracks; trk++) {
td = &cdu->toc[trk];
if (pos < td->address) {
if (trk > 0)
td--;
break;
}
if (pos >= td->address && pos < td[1].address)
break;
}
if (!td)
return 0;
getsub_deinterleaved (subbuf, cdu, td, pos);
if (all) {
memcpy(buf, subbuf, SUB_CHANNEL_SIZE);
} else {
memcpy (p, subbuf + 12, 12);
}
if (cdu->cdda_play_state == AUDIO_STATUS_PLAY_COMPLETE || cdu->cdda_play_state == AUDIO_STATUS_PLAY_ERROR)
cdu->cdda_play_state = AUDIO_STATUS_NO_STATUS;
return 1;
}
static uae_u32 command_volume (int unitnum, uae_u16 volume_left, uae_u16 volume_right)
{
struct cdunit *cdu = unitisopen (unitnum);
if (!cdu)
return -1;
uae_u32 old = (cdu->cdda_volume[1] << 16) | (cdu->cdda_volume[0] << 0);
cdu->cdda_volume[0] = volume_left;
cdu->cdda_volume[1] = volume_right;
return old;
}
extern void encode_l2 (uae_u8 *p, int address);
static int command_rawread (int unitnum, uae_u8 *data, int sector, int size, int sectorsize, uae_u32 extra)
{
int ret = 0;
struct cdunit *cdu = unitisopen (unitnum);
if (!cdu)
return 0;
int asector = sector;
struct cdtoc *t = findtoc (cdu, §or, true);
int ssize;
if (!t)
goto end;
ssize = t->size + t->skipsize;
cdda_stop (cdu);
if (sectorsize > 0) {
if ((sectorsize == 2352 || sectorsize == 2368 || sectorsize == 2448) && t->size == 2336) {
// 2336 -> 2352
while (size-- > 0) {
int address = asector + 150;
if (isaudiotrack(&cdu->di.toc, sector)) {
do_read(cdu, t, data, sector, 0, t->size, true);
} else {
data[0] = 0x00;
memset(data + 1, 0xff, 11);
data[12] = tobcd((uae_u8)(address / (60 * 75)));
data[13] = tobcd((uae_u8)((address / 75) % 60));
data[14] = tobcd((uae_u8)(address % 75));
data[15] = 2; /* MODE2 */
do_read(cdu, t, data + 16, sector, 0, t->size, false);
}
sector++;
asector++;
data += sectorsize;
ret += sectorsize;
if (sectorsize == 2448) {
// all subs
getsub_deinterleaved(data - SUB_CHANNEL_SIZE, cdu, t, sector);
} else if (sectorsize == 2368) {
// sub q only
uae_u8 subs[SUB_CHANNEL_SIZE];
getsub_deinterleaved(subs, cdu, t, sector);
memcpy(data - SUBQ_SIZE, subs + SUBQ_SIZE, SUBQ_SIZE);
}
}
} else if ((sectorsize == 2352 || sectorsize == 2368 || sectorsize == 2448) && t->size == 2048) {
// 2048 -> 2352
while (size-- > 0) {
memset (data, 0, 16);
do_read (cdu, t, data + 16, sector, 0, 2048, false);
encode_l2 (data, sector + 150);
sector++;
asector++;
data += sectorsize;
ret += sectorsize;
if (sectorsize == 2448) {
// all subs
getsub_deinterleaved(data - SUB_CHANNEL_SIZE, cdu, t, sector);
} else if (sectorsize == 2368) {
// sub q only
uae_u8 subs[SUB_CHANNEL_SIZE];
getsub_deinterleaved(subs, cdu, t, sector);
memcpy(data - SUBQ_SIZE, subs + SUBQ_SIZE, SUBQ_SIZE);
}
}
} else if (sectorsize == 2048 && t->size == 2352) {
// 2352 -> 2048
while (size-- > 0) {
uae_u8 b = 0;
do_read (cdu, t, &b, sector, 15, 1, false);
do_read (cdu, t, data, sector, b == 2 ? 24 : 16, sectorsize, false);
sector++;
asector++;
data += sectorsize;
ret += sectorsize;
}
} else if (sectorsize == 2336 && t->size == 2352) {
// 2352 -> 2336
while (size-- > 0) {
uae_u8 b = 0;
do_read (cdu, t, &b, sector, 15, 1, false);
if (b != 2 && b != 0) // MODE0 or MODE2 only allowed
return 0;
do_read (cdu, t, data, sector, 16, sectorsize, false);
sector++;
asector++;
data += sectorsize;
ret += sectorsize;
}
} else if (sectorsize == t->size) {