Mercurial > mplayer.hg
annotate open.c @ 1905:8d773b35751e
*** empty log message ***
author | gabucino |
---|---|
date | Mon, 17 Sep 2001 18:15:18 +0000 |
parents | cf4165e34c13 |
children | 5216f108cb4f |
rev | line source |
---|---|
1467 | 1 |
2 #include <stdio.h> | |
3 #include <stdlib.h> | |
4 #include <string.h> | |
5 #include <unistd.h> | |
6 #include <fcntl.h> | |
7 | |
8 #include "config.h" | |
1567 | 9 #include "mp_msg.h" |
1584 | 10 #include "help_mp.h" |
1467 | 11 |
1468 | 12 #ifdef __FreeBSD__ |
13 #include <sys/cdrio.h> | |
14 #endif | |
15 | |
1467 | 16 #include "stream.h" |
1482 | 17 #include "demuxer.h" |
1467 | 18 |
19 #ifdef STREAMING | |
20 #include "url.h" | |
21 #include "network.h" | |
22 static URL_t* url; | |
23 #endif | |
24 | |
1596 | 25 int dvd_title=0; |
26 int dvd_chapter=1; | |
27 int dvd_angle=1; | |
28 | |
29 #ifdef USE_DVDREAD | |
30 | |
31 #include <dvdread/dvd_reader.h> | |
32 #include <dvdread/ifo_types.h> | |
33 #include <dvdread/ifo_read.h> | |
34 #include <dvdread/nav_read.h> | |
1875 | 35 |
36 #define DVDREAD_VERSION(maj,min,micro) ((maj)*10000 + (min)*100 + (micro)) | |
37 | |
38 /* | |
39 * Try to autodetect the libdvd-0.9.0 library | |
40 * (0.9.0 removed the <dvdread/dvd_udf.h> header, and moved the two defines | |
41 * DVD_VIDEO_LB_LEN and MAX_UDF_FILE_NAME_LEN from it to | |
42 * <dvdread/dvd_reader.h>) | |
43 */ | |
44 #if defined(DVD_VIDEO_LB_LEN) && defined(MAX_UDF_FILE_NAME_LEN) | |
45 #define LIBDVDREAD_VERSION DVDREAD_VERSION(0,9,0) | |
46 #else | |
47 #define LIBDVDREAD_VERSION DVDREAD_VERSION(0,8,0) | |
48 #endif | |
49 | |
1596 | 50 |
51 typedef struct { | |
52 dvd_reader_t *dvd; | |
53 dvd_file_t *title; | |
54 ifo_handle_t *vmg_file; | |
55 tt_srpt_t *tt_srpt; | |
56 ifo_handle_t *vts_file; | |
57 vts_ptt_srpt_t *vts_ptt_srpt; | |
58 pgc_t *cur_pgc; | |
59 // | |
60 int cur_cell; | |
61 int cur_pack; | |
62 int cell_last_pack; | |
63 // Navi: | |
64 int packs_left; | |
65 dsi_t dsi_pack; | |
1609
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
66 int angle_seek; |
1596 | 67 } dvd_priv_t; |
68 | |
69 #endif | |
70 | |
1467 | 71 extern int vcd_get_track_end(int fd,int track); |
72 | |
73 // Open a new stream (stdin/file/vcd/url) | |
74 | |
75 stream_t* open_stream(char* filename,int vcd_track,int* file_format){ | |
76 stream_t* stream=NULL; | |
77 int f=-1; | |
78 off_t len; | |
79 #ifdef VCD_CACHE | |
80 int vcd_cache_size=128; | |
81 #endif | |
82 #ifdef __FreeBSD__ | |
83 int bsize = VCD_SECTOR_SIZE; | |
84 #endif | |
85 | |
86 //============ Open VideoCD track ============== | |
87 if(vcd_track){ | |
88 int ret,ret2; | |
1596 | 89 if(!filename) filename=DEFAULT_CDROM_DEVICE; |
1467 | 90 f=open(filename,O_RDONLY); |
1584 | 91 if(f<0){ mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_CdDevNotfound,filename);return NULL; } |
1467 | 92 vcd_read_toc(f); |
93 ret2=vcd_get_track_end(f,vcd_track); | |
1584 | 94 if(ret2<0){ mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_ErrTrackSelect " (get)\n");return NULL;} |
1467 | 95 ret=vcd_seek_to_track(f,vcd_track); |
1584 | 96 if(ret<0){ mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_ErrTrackSelect " (seek)\n");return NULL;} |
1467 | 97 // seek_to_byte+=ret; |
1567 | 98 mp_msg(MSGT_OPEN,MSGL_V,"VCD start byte position: 0x%X end: 0x%X\n",ret,ret2); |
1467 | 99 #ifdef VCD_CACHE |
100 vcd_cache_init(vcd_cache_size); | |
101 #endif | |
102 #ifdef __FreeBSD__ | |
103 if (ioctl (f, CDRIOCSETBLOCKSIZE, &bsize) == -1) { | |
104 perror ( "Error in CDRIOCSETBLOCKSIZE"); | |
105 } | |
106 #endif | |
107 stream=new_stream(f,STREAMTYPE_VCD); | |
108 stream->start_pos=ret; | |
109 stream->end_pos=ret2; | |
110 return stream; | |
111 } | |
112 | |
1596 | 113 //============ Open DVD title ============== |
114 #ifdef USE_DVDREAD | |
115 if(dvd_title){ | |
116 int ret,ret2; | |
117 dvd_priv_t *d; | |
118 int ttn,pgc_id,pgn; | |
119 dvd_reader_t *dvd; | |
120 dvd_file_t *title; | |
121 ifo_handle_t *vmg_file; | |
122 tt_srpt_t *tt_srpt; | |
123 ifo_handle_t *vts_file; | |
124 /** | |
125 * Open the disc. | |
126 */ | |
127 if(!filename) filename=DEFAULT_DVD_DEVICE; | |
128 dvd = DVDOpen(filename); | |
129 if( !dvd ) { | |
130 mp_msg(MSGT_OPEN,MSGL_ERR, "Couldn't open DVD: %s\n",filename); | |
131 return NULL; | |
132 } | |
133 | |
134 mp_msg(MSGT_OPEN,MSGL_INFO, "Reading disc structure, please wait...\n"); | |
135 | |
136 /** | |
137 * Load the video manager to find out the information about the titles on | |
138 * this disc. | |
139 */ | |
140 vmg_file = ifoOpen( dvd, 0 ); | |
141 if( !vmg_file ) { | |
142 mp_msg(MSGT_OPEN,MSGL_ERR, "Can't open VMG info!\n"); | |
143 DVDClose( dvd ); | |
144 return NULL; | |
145 } | |
146 tt_srpt = vmg_file->tt_srpt; | |
147 /** | |
148 * Make sure our title number is valid. | |
149 */ | |
150 mp_msg(MSGT_OPEN,MSGL_INFO, "There are %d titles on this DVD.\n", | |
151 tt_srpt->nr_of_srpts ); | |
152 if( dvd_title < 1 || dvd_title > tt_srpt->nr_of_srpts ) { | |
153 mp_msg(MSGT_OPEN,MSGL_ERR, "Invalid DVD title number: %d\n", dvd_title); | |
154 ifoClose( vmg_file ); | |
155 DVDClose( dvd ); | |
156 return NULL; | |
157 } | |
158 --dvd_title; // remap 1.. -> 0.. | |
159 /** | |
160 * Make sure the chapter number is valid for this title. | |
161 */ | |
162 mp_msg(MSGT_OPEN,MSGL_INFO, "There are %d chapters in this DVD title.\n", | |
163 tt_srpt->title[dvd_title].nr_of_ptts ); | |
164 if( dvd_chapter<1 || dvd_chapter>tt_srpt->title[dvd_title].nr_of_ptts ) { | |
165 mp_msg(MSGT_OPEN,MSGL_ERR, "Invalid DVD chapter number: %d\n", dvd_chapter); | |
166 ifoClose( vmg_file ); | |
167 DVDClose( dvd ); | |
168 return NULL; | |
169 } | |
170 --dvd_chapter; // remap 1.. -> 0.. | |
171 /** | |
172 * Make sure the angle number is valid for this title. | |
173 */ | |
174 mp_msg(MSGT_OPEN,MSGL_INFO, "There are %d angles in this DVD title.\n", | |
175 tt_srpt->title[dvd_title].nr_of_angles ); | |
176 if( dvd_angle<1 || dvd_angle>tt_srpt->title[dvd_title].nr_of_angles ) { | |
177 mp_msg(MSGT_OPEN,MSGL_ERR, "Invalid DVD angle number: %d\n", dvd_angle); | |
178 ifoClose( vmg_file ); | |
179 DVDClose( dvd ); | |
180 return NULL; | |
181 } | |
1617 | 182 --dvd_angle; // remap 1.. -> 0.. |
1596 | 183 /** |
184 * Load the VTS information for the title set our title is in. | |
185 */ | |
186 vts_file = ifoOpen( dvd, tt_srpt->title[dvd_title].title_set_nr ); | |
187 if( !vts_file ) { | |
188 mp_msg(MSGT_OPEN,MSGL_ERR, "Can't open the IFO file for DVD title %d.\n", | |
189 tt_srpt->title[dvd_title].title_set_nr ); | |
190 ifoClose( vmg_file ); | |
191 DVDClose( dvd ); | |
192 return NULL; | |
193 } | |
194 /** | |
195 * We've got enough info, time to open the title set data. | |
196 */ | |
197 title = DVDOpenFile( dvd, tt_srpt->title[dvd_title].title_set_nr, | |
198 DVD_READ_TITLE_VOBS ); | |
199 if( !title ) { | |
200 mp_msg(MSGT_OPEN,MSGL_ERR, "Can't open title VOBS (VTS_%02d_1.VOB).\n", | |
201 tt_srpt->title[dvd_title].title_set_nr ); | |
202 ifoClose( vts_file ); | |
203 ifoClose( vmg_file ); | |
204 DVDClose( dvd ); | |
205 return NULL; | |
206 } | |
207 | |
208 mp_msg(MSGT_OPEN,MSGL_INFO, "DVD successfully opened!\n"); | |
209 // store data | |
210 d=malloc(sizeof(dvd_priv_t)); memset(d,0,sizeof(dvd_priv_t)); | |
211 d->dvd=dvd; | |
212 d->title=title; | |
213 d->vmg_file=vmg_file; | |
214 d->tt_srpt=tt_srpt; | |
215 d->vts_file=vts_file; | |
216 | |
217 /** | |
218 * Determine which program chain we want to watch. This is based on the | |
219 * chapter number. | |
220 */ | |
221 ttn = tt_srpt->title[ dvd_title ].vts_ttn; // local | |
222 pgc_id = vts_file->vts_ptt_srpt->title[ttn-1].ptt[dvd_chapter].pgcn; // local | |
223 pgn = vts_file->vts_ptt_srpt->title[ttn-1].ptt[dvd_chapter].pgn; // local | |
224 d->cur_pgc = vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc; | |
225 d->cur_cell = d->cur_pgc->program_map[pgn-1] - 1; // start playback here | |
226 d->packs_left=-1; // for Navi stuff | |
1609
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
227 d->angle_seek=0; |
1596 | 228 |
229 if( d->cur_pgc->cell_playback[d->cur_cell].block_type | |
230 == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle; | |
231 d->cur_pack = d->cur_pgc->cell_playback[ d->cur_cell ].first_sector; | |
232 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector; | |
233 mp_msg(MSGT_DVD,MSGL_V, "DVD start cell: %d pack: 0x%X-0x%X \n",d->cur_cell,d->cur_pack,d->cell_last_pack); | |
234 | |
235 // ... (unimplemented) | |
236 // return NULL; | |
237 stream=new_stream(-1,STREAMTYPE_DVD); | |
238 stream->start_pos=(off_t)d->cur_pack*2048; | |
239 //stream->end_pos=0; | |
240 stream->priv=(void*)d; | |
241 return stream; | |
242 } | |
243 #endif | |
244 | |
1467 | 245 //============ Open STDIN ============ |
246 if(!strcmp(filename,"-")){ | |
247 // read from stdin | |
1584 | 248 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_ReadSTDIN); |
1467 | 249 f=0; // 0=stdin |
250 stream=new_stream(f,STREAMTYPE_STREAM); | |
251 return stream; | |
252 } | |
253 | |
254 #ifdef STREAMING | |
255 url = url_new(filename); | |
256 if(url) { | |
257 (*file_format)=autodetectProtocol( url, &f ); | |
258 if( (*file_format)==DEMUXER_TYPE_UNKNOWN ) { | |
1584 | 259 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_UnableOpenURL, filename); |
1467 | 260 url_free(url); |
261 return NULL; | |
262 } | |
1899 | 263 f=streaming_start( &url, f, *file_format ); |
1584 | 264 if(f<0){ mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_UnableOpenURL, url->url); return NULL; } |
265 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_ConnToServer, url->hostname ); | |
1467 | 266 stream=new_stream(f,STREAMTYPE_STREAM); |
267 return NULL; | |
268 } | |
269 #endif | |
270 | |
271 //============ Open plain FILE ============ | |
272 f=open(filename,O_RDONLY); | |
1584 | 273 if(f<0){ mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_FileNotFound,filename);return NULL; } |
1467 | 274 len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET); |
275 if (len == -1) | |
276 perror("Error: lseek failed to obtain video file size"); | |
277 else | |
278 if(verbose) | |
279 #ifdef _LARGEFILE_SOURCE | |
1567 | 280 mp_msg(MSGT_OPEN,MSGL_V,"File size is %lld bytes\n", (long long)len); |
1467 | 281 #else |
1567 | 282 mp_msg(MSGT_OPEN,MSGL_V,"File size is %u bytes\n", (unsigned int)len); |
1467 | 283 #endif |
284 stream=new_stream(f,STREAMTYPE_FILE); | |
285 stream->end_pos=len; | |
286 return stream; | |
287 | |
288 } | |
1596 | 289 |
290 | |
291 #ifdef USE_DVDREAD | |
292 | |
293 static int dvd_next_cell(dvd_priv_t *d){ | |
294 int next_cell=d->cur_cell; | |
1616 | 295 |
296 mp_msg(MSGT_DVD,MSGL_V, "dvd_next_cell: next1=0x%X \n",next_cell); | |
1596 | 297 |
298 if( d->cur_pgc->cell_playback[ next_cell ].block_type | |
299 == BLOCK_TYPE_ANGLE_BLOCK ) { | |
300 int i; | |
301 while(next_cell<d->cur_pgc->nr_of_cells){ | |
302 if( d->cur_pgc->cell_playback[next_cell].block_mode | |
303 == BLOCK_MODE_LAST_CELL ) break; | |
304 ++next_cell; | |
305 } | |
306 } | |
1616 | 307 mp_msg(MSGT_DVD,MSGL_V, "dvd_next_cell: next2=0x%X \n",next_cell); |
1596 | 308 |
309 ++next_cell; | |
310 if(next_cell>=d->cur_pgc->nr_of_cells) return -1; // EOF | |
311 if( d->cur_pgc->cell_playback[next_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK ){ | |
312 next_cell+=dvd_angle; | |
313 if(next_cell>=d->cur_pgc->nr_of_cells) return -1; // EOF | |
314 } | |
1616 | 315 mp_msg(MSGT_DVD,MSGL_V, "dvd_next_cell: next3=0x%X \n",next_cell); |
1596 | 316 return next_cell; |
317 } | |
318 | |
319 int dvd_read_sector(dvd_priv_t *d,unsigned char* data){ | |
320 int len; | |
321 | |
322 if(d->packs_left==0){ | |
323 /** | |
324 * If we're not at the end of this cell, we can determine the next | |
325 * VOBU to display using the VOBU_SRI information section of the | |
326 * DSI. Using this value correctly follows the current angle, | |
327 * avoiding the doubled scenes in The Matrix, and makes our life | |
328 * really happy. | |
329 * | |
330 * Otherwise, we set our next address past the end of this cell to | |
331 * force the code above to go to the next cell in the program. | |
332 */ | |
333 if( d->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL ) { | |
334 d->cur_pack= d->dsi_pack.dsi_gi.nv_pck_lbn + | |
335 ( d->dsi_pack.vobu_sri.next_vobu & 0x7fffffff ); | |
336 mp_msg(MSGT_DVD,MSGL_DBG2, "Navi new pos=0x%X \n",d->cur_pack); | |
337 } else { | |
338 // end of cell! find next cell! | |
339 mp_msg(MSGT_DVD,MSGL_V, "--- END OF CELL !!! ---\n"); | |
340 d->cur_pack=d->cell_last_pack+1; | |
341 } | |
342 } | |
343 | |
344 read_next: | |
345 | |
346 if(d->cur_pack>d->cell_last_pack){ | |
347 // end of cell! | |
348 int next=dvd_next_cell(d); | |
349 if(next>=0){ | |
350 d->cur_cell=next; | |
351 | |
1615 | 352 // if( d->cur_pgc->cell_playback[d->cur_cell].block_type |
353 // == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle; | |
1596 | 354 d->cur_pack = d->cur_pgc->cell_playback[ d->cur_cell ].first_sector; |
355 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector; | |
356 mp_msg(MSGT_DVD,MSGL_V, "DVD next cell: %d pack: 0x%X-0x%X \n",d->cur_cell,d->cur_pack,d->cell_last_pack); | |
357 | |
358 } else return -1; // EOF | |
359 } | |
360 | |
361 len = DVDReadBlocks( d->title, d->cur_pack, 1, data ); | |
362 if(!len) return -1; //error | |
363 | |
364 if(data[38]==0 && data[39]==0 && data[40]==1 && data[41]==0xBF && | |
365 data[1024]==0 && data[1025]==0 && data[1026]==1 && data[1027]==0xBF){ | |
366 // found a Navi packet!!! | |
1875 | 367 #if LIBDVDREAD_VERSION >= DVDREAD_VERSION(0,9,0) |
368 navRead_DSI( &d->dsi_pack, &(data[ DSI_START_BYTE ]) ); | |
369 #else | |
1596 | 370 navRead_DSI( &d->dsi_pack, &(data[ DSI_START_BYTE ]), sizeof(dsi_t) ); |
1875 | 371 #endif |
1596 | 372 if(d->cur_pack != d->dsi_pack.dsi_gi.nv_pck_lbn ){ |
373 mp_msg(MSGT_DVD,MSGL_V, "Invalid NAVI packet! lba=0x%X navi=0x%X \n", | |
374 d->cur_pack,d->dsi_pack.dsi_gi.nv_pck_lbn); | |
375 } else { | |
376 // process! | |
377 d->packs_left = d->dsi_pack.dsi_gi.vobu_ea; | |
378 mp_msg(MSGT_DVD,MSGL_DBG2, "Found NAVI packet! lba=0x%X len=%d \n",d->cur_pack,d->packs_left); | |
1609
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
379 if(d->angle_seek){ |
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
380 int skip=d->dsi_pack.sml_agli.data[dvd_angle].address; |
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
381 if(skip) d->cur_pack=d->dsi_pack.dsi_gi.nv_pck_lbn+skip; |
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
382 d->angle_seek=0; |
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
383 mp_msg(MSGT_DVD,MSGL_V, "Angle-seek synced! skip=%d new_lba=0x%X \n",skip,d->cur_pack); |
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
384 } |
1596 | 385 } |
386 ++d->cur_pack; | |
387 goto read_next; | |
388 } | |
389 | |
390 ++d->cur_pack; | |
391 if(d->packs_left>=0) --d->packs_left; | |
1609
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
392 |
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
393 if(d->angle_seek) goto read_next; // searching for Navi packet |
1596 | 394 |
395 return d->cur_pack-1; | |
396 } | |
397 | |
398 void dvd_seek(dvd_priv_t *d,int pos){ | |
399 d->packs_left=-1; | |
400 d->cur_pack=pos; | |
401 | |
402 // check if we stay in current cell (speedup things, and avoid angle skip) | |
1609
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
403 if(d->cur_pack>d->cell_last_pack || |
1596 | 404 d->cur_pack<d->cur_pgc->cell_playback[ d->cur_cell ].first_sector){ |
405 | |
406 // ok, cell change, find the right cell! | |
407 d->cur_cell=0; | |
408 if( d->cur_pgc->cell_playback[d->cur_cell].block_type | |
409 == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle; | |
410 | |
411 while(1){ | |
412 int next; | |
413 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector; | |
414 if(d->cur_pack<d->cur_pgc->cell_playback[ d->cur_cell ].first_sector){ | |
415 d->cur_pack=d->cur_pgc->cell_playback[ d->cur_cell ].first_sector; | |
416 break; | |
417 } | |
418 if(d->cur_pack<=d->cell_last_pack) break; // ok, we find it! :) | |
419 next=dvd_next_cell(d); | |
420 if(next<0){ | |
421 // d->cur_pack=d->cell_last_pack+1; | |
422 break; // we're after the last cell | |
423 } | |
424 d->cur_cell=next; | |
425 } | |
1609
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
426 |
1596 | 427 } |
428 | |
429 mp_msg(MSGT_DVD,MSGL_V, "DVD Seek! lba=0x%X cell=%d packs: 0x%X-0x%X \n", | |
430 d->cur_pack,d->cur_cell,d->cur_pgc->cell_playback[ d->cur_cell ].first_sector,d->cell_last_pack); | |
431 | |
1609
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
432 // if we're in interleaved multi-angle cell, find the right angle chain! |
1596 | 433 // (read Navi block, and use the seamless angle jump table) |
1609
84199d075839
ugly seeking bug fixed, correct multiangle seek implemented
arpi
parents:
1596
diff
changeset
|
434 d->angle_seek=1; |
1596 | 435 |
436 } | |
437 | |
438 #endif |