sync with en/mplayer.1 rev. 30936
[mplayer/glamo.git] / stream / stream_vcd.c
blob819c3e52ea20d49bb7130449b105c1bde447b873
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #if defined(__MINGW32__) || defined(__CYGWIN__)
22 #include <windows.h>
23 #endif
25 #include "osdep/osdep.h"
27 #include "mp_msg.h"
28 #include "stream.h"
29 #include "help_mp.h"
30 #include "m_option.h"
31 #include "m_struct.h"
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
37 #include <sys/ioctl.h>
38 #endif
39 #include <errno.h>
41 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
42 #include "vcd_read_fbsd.h"
43 #elif defined(__APPLE__)
44 #include "vcd_read_darwin.h"
45 #elif defined(__MINGW32__) || defined(__CYGWIN__)
46 #include "vcd_read_win32.h"
47 #elif defined(__OS2__)
48 #include "vcd_read_os2.h"
49 #else
50 #include "vcd_read.h"
51 #endif
53 #include "libmpdemux/demuxer.h"
55 extern char *cdrom_device;
57 static struct stream_priv_s {
58 int track;
59 char* device;
60 } stream_priv_dflts = {
62 NULL
65 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
66 /// URL definition
67 static const m_option_t stream_opts_fields[] = {
68 { "track", ST_OFF(track), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
69 { "device", ST_OFF(device), CONF_TYPE_STRING, 0, 0 ,0, NULL},
70 /// For url parsing
71 { "hostname", ST_OFF(track), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
72 { "filename", ST_OFF(device), CONF_TYPE_STRING, 0, 0 ,0, NULL},
73 { NULL, NULL, 0, 0, 0, 0, NULL }
75 static const struct m_struct_st stream_opts = {
76 "vcd",
77 sizeof(struct stream_priv_s),
78 &stream_priv_dflts,
79 stream_opts_fields
82 static int fill_buffer(stream_t *s, char* buffer, int max_len){
83 if(s->pos > s->end_pos) /// don't past end of current track
84 return 0;
85 return vcd_read(s->priv,buffer);
88 static int seek(stream_t *s,off_t newpos) {
89 s->pos = newpos;
90 vcd_set_msf(s->priv,s->pos/VCD_SECTOR_DATA);
91 return 1;
94 static void close_s(stream_t *stream) {
95 free(stream->priv);
98 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
99 struct stream_priv_s* p = opts;
100 int ret,ret2,f,sect,tmp;
101 mp_vcd_priv_t* vcd;
102 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
103 int bsize = VCD_SECTOR_SIZE;
104 #endif
105 #if defined(__MINGW32__) || defined(__CYGWIN__)
106 HANDLE hd;
107 char device[] = "\\\\.\\?:";
108 #endif
109 #if defined(__OS2__)
110 char device[] = "X:";
111 HFILE hcd;
112 ULONG ulAction;
113 ULONG rc;
114 #endif
116 if(mode != STREAM_READ
117 #if defined(__MINGW32__) || defined(__CYGWIN__)
118 || GetVersion() > 0x80000000 // Win9x
119 #endif
121 m_struct_free(&stream_opts,opts);
122 return STREAM_UNSUPPORTED;
125 if (!p->device) {
126 if(cdrom_device)
127 p->device = strdup(cdrom_device);
128 else
129 p->device = strdup(DEFAULT_CDROM_DEVICE);
132 #if defined(__MINGW32__) || defined(__CYGWIN__)
133 device[4] = p->device[0];
134 /* open() can't be used for devices so do it the complicated way */
135 hd = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
136 OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
137 f = _open_osfhandle((long)hd, _O_RDONLY);
138 #elif defined(__OS2__)
139 device[0] = p->device[0];
140 rc = DosOpen(device, &hcd, &ulAction, 0, FILE_NORMAL,
141 OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
142 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_DASD,
143 NULL);
144 f = rc ? -1 : hcd;
145 #else
146 f=open(p->device,O_RDONLY);
147 #endif
148 if(f<0){
149 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_CdDevNotfound,p->device);
150 m_struct_free(&stream_opts,opts);
151 return STREAM_ERROR;
154 vcd = vcd_read_toc(f);
155 if(!vcd) {
156 mp_msg(MSGT_OPEN,MSGL_ERR,"Failed to get cd toc\n");
157 close(f);
158 m_struct_free(&stream_opts,opts);
159 return STREAM_ERROR;
161 ret2=vcd_get_track_end(vcd,p->track);
162 if(ret2<0){
163 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_ErrTrackSelect " (get)\n");
164 close(f);
165 free(vcd);
166 m_struct_free(&stream_opts,opts);
167 return STREAM_ERROR;
169 ret=vcd_seek_to_track(vcd,p->track);
170 if(ret<0){
171 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_ErrTrackSelect " (seek)\n");
172 close(f);
173 free(vcd);
174 m_struct_free(&stream_opts,opts);
175 return STREAM_ERROR;
177 /* search forward up to at most 3 seconds to skip leading margin */
178 sect = ret / VCD_SECTOR_DATA;
179 for (tmp = sect; tmp < sect + 3 * 75; tmp++) {
180 char mem[VCD_SECTOR_DATA];
181 //since MPEG packs are block-aligned we stop discarding sectors if they are non-null
182 if (vcd_read(vcd, mem) != VCD_SECTOR_DATA || mem[2] || mem[3])
183 break;
185 mp_msg(MSGT_OPEN, MSGL_DBG2, "%d leading sectors skipped\n", tmp - sect);
186 vcd_set_msf(vcd, tmp);
187 ret = tmp * VCD_SECTOR_DATA;
189 mp_msg(MSGT_OPEN,MSGL_V,"VCD start byte position: 0x%X end: 0x%X\n",ret,ret2);
191 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
192 if (ioctl (f, CDRIOCSETBLOCKSIZE, &bsize) == -1) {
193 mp_msg(MSGT_OPEN,MSGL_WARN,"Error in CDRIOCSETBLOCKSIZE");
195 #endif
197 stream->fd = f;
198 stream->type = STREAMTYPE_VCD;
199 stream->sector_size = VCD_SECTOR_DATA;
200 stream->start_pos=ret;
201 stream->end_pos=ret2;
202 stream->priv = vcd;
204 stream->fill_buffer = fill_buffer;
205 stream->seek = seek;
206 stream->close = close_s;
207 *file_format = DEMUXER_TYPE_MPEG_PS;
209 m_struct_free(&stream_opts,opts);
210 return STREAM_OK;
213 const stream_info_t stream_info_vcd = {
214 "Video CD",
215 "vcd",
216 "Albeu",
217 "based on the code from ???",
218 open_s,
219 { "vcd", NULL },
220 &stream_opts,
221 1 // Urls are an option string