aboutsummaryrefslogtreecommitdiffhomepage
path: root/stream/vcd_read_os2.h
blob: 0585d1d7d08339616c03ab14ae1d7de070b3728a (plain)
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
/*
 * implementation of VCD IO for OS/2
 *
 * Copyright (c) 2009 KO Myung-Hun (komh@chollian.net)
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPLAYER_VCD_READ_OS2_H
#define MPLAYER_VCD_READ_OS2_H

#include "mp_msg.h"

struct __attribute__((packed)) msf {
    BYTE bFrame;
    BYTE bSecond;
    BYTE bMinute;
    BYTE bReserved;
};

typedef struct {
    HFILE      hcd;
    struct msf msfCurrent;
    int        iFirstTrack;
    int        iLastTrack;
    struct msf msfLeadOut;
    BYTE       abVCDSector[VCD_SECTOR_SIZE];
} mp_vcd_priv_t;

static inline void vcd_set_msf(mp_vcd_priv_t *vcd, unsigned sect)
{
    sect += 150;
    vcd->msfCurrent.bFrame = sect % 75;
    sect = sect / 75;
    vcd->msfCurrent.bSecond = sect % 60;
    sect = sect / 60;
    vcd->msfCurrent.bMinute = sect;
}

static inline unsigned vcd_get_msf(mp_vcd_priv_t *vcd)
{
  return vcd->msfCurrent.bFrame  +
        (vcd->msfCurrent.bSecond + vcd->msfCurrent.bMinute * 60) * 75 - 150;
}

int vcd_seek_to_track(mp_vcd_priv_t *vcd, int track)
{
    struct {
        UCHAR auchSign[4];
        BYTE  bTrack;
    } __attribute__((packed)) sParam = {{'C', 'D', '0', '1'},};

    struct {
        struct msf msfStart;
        BYTE       bControlInfo;
    } __attribute__((packed)) sData;

    ULONG ulParamLen;
    ULONG ulDataLen;
    ULONG rc;

    sParam.bTrack = track;
    rc = DosDevIOCtl(vcd->hcd, IOCTL_CDROMAUDIO, CDROMAUDIO_GETAUDIOTRACK,
                     &sParam, sizeof(sParam), &ulParamLen,
                     &sData, sizeof(sData), &ulDataLen);
    if (rc) {
        mp_msg(MSGT_STREAM, MSGL_ERR, "DosDevIOCtl(GETAUDIOTRACK) = 0x%lx\n", rc);
        return -1;
    }

    vcd->msfCurrent = sData.msfStart;

    return VCD_SECTOR_DATA * vcd_get_msf(vcd);
}

static int vcd_get_track_end(mp_vcd_priv_t *vcd, int track)
{
    if (track < vcd->iLastTrack)
        return vcd_seek_to_track(vcd, track + 1);

    vcd->msfCurrent = vcd->msfLeadOut;

    return VCD_SECTOR_DATA * vcd_get_msf(vcd);
}

static mp_vcd_priv_t *vcd_read_toc(int fd)
{
    mp_vcd_priv_t *vcd;

    UCHAR auchParamDisk[4] = {'C', 'D', '0', '1'};

    struct {
        BYTE       bFirstTrack;
        BYTE       bLastTrack;
        struct msf msfLeadOut;
    } __attribute__((packed)) sDataDisk;

    struct {
        UCHAR auchSign[4];
        BYTE  bTrack;
    } __attribute__((packed)) sParamTrack = {{'C', 'D', '0', '1'},};

    struct {
        struct msf msfStart;
        BYTE       bControlInfo;
    } __attribute__((packed)) sDataTrack;

    ULONG ulParamLen;
    ULONG ulDataLen;
    ULONG rc;
    int   i, iMinute = 0, iSecond = 0, iFrame = 0;

    rc = DosDevIOCtl(fd, IOCTL_CDROMAUDIO, CDROMAUDIO_GETAUDIODISK,
                     auchParamDisk, sizeof(auchParamDisk), &ulParamLen,
                     &sDataDisk, sizeof(sDataDisk), &ulDataLen);
    if (rc) {
        mp_msg(MSGT_OPEN, MSGL_ERR, "DosDevIOCtl(GETAUDIODISK) = 0x%lx\n", rc);
        return NULL;
    }

    mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_START_TRACK=%d\n", sDataDisk.bFirstTrack);
    mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_END_TRACK=%d\n", sDataDisk.bLastTrack);

    for (i = sDataDisk.bFirstTrack; i <= sDataDisk.bLastTrack + 1; i++) {
        if (i <= sDataDisk.bLastTrack) {
            sParamTrack.bTrack = i;
            rc = DosDevIOCtl(fd, IOCTL_CDROMAUDIO, CDROMAUDIO_GETAUDIOTRACK,
                             &sParamTrack, sizeof(sParamTrack), &ulParamLen,
                             &sDataTrack, sizeof(sDataTrack), &ulDataLen);
            if (rc) {
                mp_msg(MSGT_OPEN, MSGL_ERR, "DosDevIOCtl(GETAUDIOTRACK) = 0x%lx\n", rc);
                return NULL;
            }

            mp_msg(MSGT_OPEN, MSGL_INFO, "track %02d:  adr=%d  ctrl=%d  %02d:%02d:%02d\n",
                   i,
                   sDataTrack.bControlInfo & 0x0F,
                   sDataTrack.bControlInfo >> 4,
                   sDataTrack.msfStart.bMinute,
                   sDataTrack.msfStart.bSecond,
                   sDataTrack.msfStart.bFrame);
        } else
            sDataTrack.msfStart = sDataDisk.msfLeadOut;

        if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO)) {
            if (i > sDataDisk.bFirstTrack) {
                iMinute = sDataTrack.msfStart.bMinute - iMinute;
                iSecond = sDataTrack.msfStart.bSecond - iSecond;
                iFrame  = sDataTrack.msfStart.bFrame  - iFrame;
                if (iFrame < 0) {
                    iFrame += 75;
                    iSecond--;
                }
                if (iSecond < 0) {
                    iSecond += 60;
                    iMinute--;
                }
                mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_TRACK_%d_MSF=%02d:%02d:%02d\n",
                       i - 1, iMinute, iSecond, iFrame);
            }

            iMinute = sDataTrack.msfStart.bMinute;
            iSecond = sDataTrack.msfStart.bSecond;
            iFrame  = sDataTrack.msfStart.bFrame;
        }
    }

    vcd                 = calloc(1, sizeof(mp_vcd_priv_t));
    vcd->hcd            = fd;
    vcd->iFirstTrack    = sDataDisk.bFirstTrack;
    vcd->iLastTrack     = sDataDisk.bLastTrack;
    vcd->msfLeadOut     = sDataDisk.msfLeadOut;

    return vcd;
}

static int vcd_end_track(mp_vcd_priv_t* vcd)
{
    return vcd->iLastTrack;
}

static int vcd_read(mp_vcd_priv_t *vcd, char *mem)
{
    struct {
        UCHAR      auchSign[4];
        BYTE       bAddrMode;
        USHORT     usSectors;
        struct msf msfStart;
        BYTE       bReserved;
        BYTE       bInterleavedSize;
    } __attribute__((packed)) sParam = {{'C', 'D', '0', '1'}, 1, 1,};

    ULONG ulParamLen;
    ULONG ulDataLen;
    ULONG rc;

    /* lead-out ? */
    if (vcd->msfCurrent.bMinute == vcd->msfLeadOut.bMinute &&
        vcd->msfCurrent.bSecond == vcd->msfLeadOut.bSecond &&
        vcd->msfCurrent.bFrame  == vcd->msfLeadOut.bFrame)
        return 0;

    sParam.msfStart = vcd->msfCurrent;
    rc = DosDevIOCtl(vcd->hcd, IOCTL_CDROMDISK, CDROMDISK_READLONG,
                     &sParam, sizeof(sParam), &ulParamLen,
                     vcd->abVCDSector, sizeof(vcd->abVCDSector), &ulDataLen);
    if (rc) {
        mp_msg(MSGT_STREAM, MSGL_ERR, "DosDevIOCtl(READLONG) = 0x%lx\n", rc);
        return 0;
    }

    memcpy(mem, &vcd->abVCDSector[VCD_SECTOR_OFFS], VCD_SECTOR_DATA);

    vcd->msfCurrent.bFrame++;
    if (vcd->msfCurrent.bFrame == 75) {
        vcd->msfCurrent.bFrame = 0;
        vcd->msfCurrent.bSecond++;
        if (vcd->msfCurrent.bSecond == 60) {
            vcd->msfCurrent.bSecond = 0;
            vcd->msfCurrent.bMinute++;
        }
    }

    return VCD_SECTOR_DATA;
}

#endif /* MPLAYER_VCD_READ_OS2_H */