Makefile: remove outdated vf_halfpack entry
[mplayer/kovensky.git] / stream / stream_vcd.c
blob23722d24e725b59c2397bf34258ed49e81d05cbf
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 "mp_msg.h"
26 #include "stream.h"
27 #include "help_mp.h"
28 #include "m_option.h"
29 #include "m_struct.h"
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
35 #include <sys/ioctl.h>
36 #endif
37 #include <errno.h>
39 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
40 #include "vcd_read_fbsd.h"
41 #elif defined(__APPLE__)
42 #include "vcd_read_darwin.h"
43 #elif defined(__MINGW32__) || defined(__CYGWIN__)
44 #include "vcd_read_win32.h"
45 #else
46 #include "vcd_read.h"
47 #endif
49 #include "libmpdemux/demuxer.h"
51 extern char *cdrom_device;
53 static struct stream_priv_s {
54 int track;
55 char* device;
56 } stream_priv_dflts = {
58 NULL
61 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
62 /// URL definition
63 static const m_option_t stream_opts_fields[] = {
64 { "track", ST_OFF(track), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
65 { "device", ST_OFF(device), CONF_TYPE_STRING, 0, 0 ,0, NULL},
66 /// For url parsing
67 { "hostname", ST_OFF(track), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
68 { "filename", ST_OFF(device), CONF_TYPE_STRING, 0, 0 ,0, NULL},
69 { NULL, NULL, 0, 0, 0, 0, NULL }
71 static const struct m_struct_st stream_opts = {
72 "vcd",
73 sizeof(struct stream_priv_s),
74 &stream_priv_dflts,
75 stream_opts_fields
78 static int fill_buffer(stream_t *s, char* buffer, int max_len){
79 if(s->pos > s->end_pos) /// don't past end of current track
80 return 0;
81 return vcd_read(s->priv,buffer);
84 static int seek(stream_t *s,off_t newpos) {
85 s->pos = newpos;
86 vcd_set_msf(s->priv,s->pos/VCD_SECTOR_DATA);
87 return 1;
90 static void close_s(stream_t *stream) {
91 free(stream->priv);
94 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
95 struct stream_priv_s* p = (struct stream_priv_s*)opts;
96 int ret,ret2,f,sect,tmp;
97 mp_vcd_priv_t* vcd;
98 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
99 int bsize = VCD_SECTOR_SIZE;
100 #endif
101 #if defined(__MINGW32__) || defined(__CYGWIN__)
102 HANDLE hd;
103 char device[] = "\\\\.\\?:";
104 #endif
106 if(mode != STREAM_READ
107 #if defined(__MINGW32__) || defined(__CYGWIN__)
108 || GetVersion() > 0x80000000 // Win9x
109 #endif
111 m_struct_free(&stream_opts,opts);
112 return STREAM_UNSUPPORTED;
115 if (!p->device) {
116 if(cdrom_device)
117 p->device = strdup(cdrom_device);
118 else
119 p->device = strdup(DEFAULT_CDROM_DEVICE);
122 #if defined(__MINGW32__) || defined(__CYGWIN__)
123 device[4] = p->device[0];
124 /* open() can't be used for devices so do it the complicated way */
125 hd = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
126 OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
127 f = _open_osfhandle((long)hd, _O_RDONLY);
128 #else
129 f=open(p->device,O_RDONLY);
130 #endif
131 if(f<0){
132 mp_tmsg(MSGT_OPEN,MSGL_ERR,"CD-ROM Device '%s' not found.\n",p->device);
133 m_struct_free(&stream_opts,opts);
134 return STREAM_ERROR;
137 vcd = vcd_read_toc(f);
138 if(!vcd) {
139 mp_msg(MSGT_OPEN,MSGL_ERR,"Failed to get cd toc\n");
140 close(f);
141 m_struct_free(&stream_opts,opts);
142 return STREAM_ERROR;
144 ret2=vcd_get_track_end(vcd,p->track);
145 if(ret2<0){
146 mp_msg(MSGT_OPEN, MSGL_ERR, "%s (get)\n",
147 mp_gtext("Error selecting VCD track."));
148 close(f);
149 free(vcd);
150 m_struct_free(&stream_opts,opts);
151 return STREAM_ERROR;
153 ret=vcd_seek_to_track(vcd,p->track);
154 if(ret<0){
155 mp_msg(MSGT_OPEN, MSGL_ERR, "%s (seek)\n",
156 mp_gtext("Error selecting VCD track."));
157 close(f);
158 free(vcd);
159 m_struct_free(&stream_opts,opts);
160 return STREAM_ERROR;
162 /* search forward up to at most 3 seconds to skip leading margin */
163 sect = ret / VCD_SECTOR_DATA;
164 for (tmp = sect; tmp < sect + 3 * 75; tmp++) {
165 char mem[VCD_SECTOR_DATA];
166 //since MPEG packs are block-aligned we stop discarding sectors if they are non-null
167 if (vcd_read(vcd, mem) != VCD_SECTOR_DATA || mem[2] || mem[3])
168 break;
170 mp_msg(MSGT_OPEN, MSGL_DBG2, "%d leading sectors skipped\n", tmp - sect);
171 vcd_set_msf(vcd, tmp);
172 ret = tmp * VCD_SECTOR_DATA;
174 mp_msg(MSGT_OPEN,MSGL_V,"VCD start byte position: 0x%X end: 0x%X\n",ret,ret2);
176 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
177 if (ioctl (f, CDRIOCSETBLOCKSIZE, &bsize) == -1) {
178 mp_msg(MSGT_OPEN,MSGL_WARN,"Error in CDRIOCSETBLOCKSIZE");
180 #endif
182 stream->fd = f;
183 stream->type = STREAMTYPE_VCD;
184 stream->sector_size = VCD_SECTOR_DATA;
185 stream->start_pos=ret;
186 stream->end_pos=ret2;
187 stream->priv = vcd;
189 stream->fill_buffer = fill_buffer;
190 stream->seek = seek;
191 stream->close = close_s;
192 *file_format = DEMUXER_TYPE_MPEG_PS;
194 m_struct_free(&stream_opts,opts);
195 return STREAM_OK;
198 const stream_info_t stream_info_vcd = {
199 "Video CD",
200 "vcd",
201 "Albeu",
202 "based on the code from ???",
203 open_s,
204 { "vcd", NULL },
205 &stream_opts,
206 1 // Urls are an option string