comparison libmpdemux/open.c @ 2310:9e059416eea6

libdemuxer...
author arpi
date Sat, 20 Oct 2001 18:49:08 +0000
parents open.c@18f4dd5d568f
children d0e1c32ad432
comparison
equal deleted inserted replaced
2309:3128b9d8b4ea 2310:9e059416eea6
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"
9 #include "mp_msg.h"
10 #include "help_mp.h"
11
12 #ifdef __FreeBSD__
13 #include <sys/cdrio.h>
14 #endif
15
16 #include "stream.h"
17 #include "demuxer.h"
18
19 #ifdef STREAMING
20 #include "url.h"
21 #include "network.h"
22 static URL_t* url;
23 #endif
24
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>
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
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;
66 int angle_seek;
67 } dvd_priv_t;
68
69 #endif
70
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;
89 if(!filename) filename=DEFAULT_CDROM_DEVICE;
90 f=open(filename,O_RDONLY);
91 if(f<0){ mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_CdDevNotfound,filename);return NULL; }
92 vcd_read_toc(f);
93 ret2=vcd_get_track_end(f,vcd_track);
94 if(ret2<0){ mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_ErrTrackSelect " (get)\n");return NULL;}
95 ret=vcd_seek_to_track(f,vcd_track);
96 if(ret<0){ mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_ErrTrackSelect " (seek)\n");return NULL;}
97 // seek_to_byte+=ret;
98 mp_msg(MSGT_OPEN,MSGL_V,"VCD start byte position: 0x%X end: 0x%X\n",ret,ret2);
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
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,MSGTR_CantOpenDVD,filename);
131 return NULL;
132 }
133
134 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_DVDwait);
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, MSGTR_DVDnumTitles,
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, MSGTR_DVDinvalidTitle, 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, MSGTR_DVDnumChapters,
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, MSGTR_DVDinvalidChapter, 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, MSGTR_DVDnumAngles,
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, MSGTR_DVDinvalidAngle, dvd_angle);
178 ifoClose( vmg_file );
179 DVDClose( dvd );
180 return NULL;
181 }
182 --dvd_angle; // remap 1.. -> 0..
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, MSGTR_DVDnoIFO,
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, MSGTR_DVDnoVOBs,
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, MSGTR_DVDopenOk);
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
227 d->angle_seek=0;
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
245 //============ Open STDIN ============
246 if(!strcmp(filename,"-")){
247 // read from stdin
248 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_ReadSTDIN);
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 ) {
259 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_UnableOpenURL, filename);
260 url_free(url);
261 return NULL;
262 }
263 f=streaming_start( &url, f, *file_format );
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 );
266 stream=new_stream(f,STREAMTYPE_STREAM);
267 return NULL;
268 }
269 #endif
270
271 //============ Open plain FILE ============
272 f=open(filename,O_RDONLY);
273 if(f<0){ mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_FileNotFound,filename);return NULL; }
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
280 mp_msg(MSGT_OPEN,MSGL_V,"File size is %lld bytes\n", (long long)len);
281 #else
282 mp_msg(MSGT_OPEN,MSGL_V,"File size is %u bytes\n", (unsigned int)len);
283 #endif
284 stream=new_stream(f,STREAMTYPE_FILE);
285 stream->end_pos=len;
286 return stream;
287
288 }
289
290
291 #ifdef USE_DVDREAD
292
293 static int dvd_next_cell(dvd_priv_t *d){
294 int next_cell=d->cur_cell;
295
296 mp_msg(MSGT_DVD,MSGL_V, "dvd_next_cell: next1=0x%X \n",next_cell);
297
298 if( d->cur_pgc->cell_playback[ next_cell ].block_type
299 == BLOCK_TYPE_ANGLE_BLOCK ) {
300 while(next_cell<d->cur_pgc->nr_of_cells){
301 if( d->cur_pgc->cell_playback[next_cell].block_mode
302 == BLOCK_MODE_LAST_CELL ) break;
303 ++next_cell;
304 }
305 }
306 mp_msg(MSGT_DVD,MSGL_V, "dvd_next_cell: next2=0x%X \n",next_cell);
307
308 ++next_cell;
309 if(next_cell>=d->cur_pgc->nr_of_cells) return -1; // EOF
310 if( d->cur_pgc->cell_playback[next_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK ){
311 next_cell+=dvd_angle;
312 if(next_cell>=d->cur_pgc->nr_of_cells) return -1; // EOF
313 }
314 mp_msg(MSGT_DVD,MSGL_V, "dvd_next_cell: next3=0x%X \n",next_cell);
315 return next_cell;
316 }
317
318 int dvd_read_sector(dvd_priv_t *d,unsigned char* data){
319 int len;
320
321 if(d->packs_left==0){
322 /**
323 * If we're not at the end of this cell, we can determine the next
324 * VOBU to display using the VOBU_SRI information section of the
325 * DSI. Using this value correctly follows the current angle,
326 * avoiding the doubled scenes in The Matrix, and makes our life
327 * really happy.
328 *
329 * Otherwise, we set our next address past the end of this cell to
330 * force the code above to go to the next cell in the program.
331 */
332 if( d->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL ) {
333 d->cur_pack= d->dsi_pack.dsi_gi.nv_pck_lbn +
334 ( d->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
335 mp_msg(MSGT_DVD,MSGL_DBG2, "Navi new pos=0x%X \n",d->cur_pack);
336 } else {
337 // end of cell! find next cell!
338 mp_msg(MSGT_DVD,MSGL_V, "--- END OF CELL !!! ---\n");
339 d->cur_pack=d->cell_last_pack+1;
340 }
341 }
342
343 read_next:
344
345 if(d->cur_pack>d->cell_last_pack){
346 // end of cell!
347 int next=dvd_next_cell(d);
348 if(next>=0){
349 d->cur_cell=next;
350
351 // if( d->cur_pgc->cell_playback[d->cur_cell].block_type
352 // == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle;
353 d->cur_pack = d->cur_pgc->cell_playback[ d->cur_cell ].first_sector;
354 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector;
355 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);
356
357 } else return -1; // EOF
358 }
359
360 len = DVDReadBlocks( d->title, d->cur_pack, 1, data );
361 if(!len) return -1; //error
362
363 if(data[38]==0 && data[39]==0 && data[40]==1 && data[41]==0xBF &&
364 data[1024]==0 && data[1025]==0 && data[1026]==1 && data[1027]==0xBF){
365 // found a Navi packet!!!
366 #if LIBDVDREAD_VERSION >= DVDREAD_VERSION(0,9,0)
367 navRead_DSI( &d->dsi_pack, &(data[ DSI_START_BYTE ]) );
368 #else
369 navRead_DSI( &d->dsi_pack, &(data[ DSI_START_BYTE ]), sizeof(dsi_t) );
370 #endif
371 if(d->cur_pack != d->dsi_pack.dsi_gi.nv_pck_lbn ){
372 mp_msg(MSGT_DVD,MSGL_V, "Invalid NAVI packet! lba=0x%X navi=0x%X \n",
373 d->cur_pack,d->dsi_pack.dsi_gi.nv_pck_lbn);
374 } else {
375 // process!
376 d->packs_left = d->dsi_pack.dsi_gi.vobu_ea;
377 mp_msg(MSGT_DVD,MSGL_DBG2, "Found NAVI packet! lba=0x%X len=%d \n",d->cur_pack,d->packs_left);
378 if(d->angle_seek){
379 int skip=d->dsi_pack.sml_agli.data[dvd_angle].address;
380 if(skip) d->cur_pack=d->dsi_pack.dsi_gi.nv_pck_lbn+skip;
381 d->angle_seek=0;
382 mp_msg(MSGT_DVD,MSGL_V, "Angle-seek synced! skip=%d new_lba=0x%X \n",skip,d->cur_pack);
383 }
384 }
385 ++d->cur_pack;
386 goto read_next;
387 }
388
389 ++d->cur_pack;
390 if(d->packs_left>=0) --d->packs_left;
391
392 if(d->angle_seek) goto read_next; // searching for Navi packet
393
394 return d->cur_pack-1;
395 }
396
397 void dvd_seek(dvd_priv_t *d,int pos){
398 d->packs_left=-1;
399 d->cur_pack=pos;
400
401 // check if we stay in current cell (speedup things, and avoid angle skip)
402 if(d->cur_pack>d->cell_last_pack ||
403 d->cur_pack<d->cur_pgc->cell_playback[ d->cur_cell ].first_sector){
404
405 // ok, cell change, find the right cell!
406 d->cur_cell=0;
407 if( d->cur_pgc->cell_playback[d->cur_cell].block_type
408 == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle;
409
410 while(1){
411 int next;
412 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector;
413 if(d->cur_pack<d->cur_pgc->cell_playback[ d->cur_cell ].first_sector){
414 d->cur_pack=d->cur_pgc->cell_playback[ d->cur_cell ].first_sector;
415 break;
416 }
417 if(d->cur_pack<=d->cell_last_pack) break; // ok, we find it! :)
418 next=dvd_next_cell(d);
419 if(next<0){
420 // d->cur_pack=d->cell_last_pack+1;
421 break; // we're after the last cell
422 }
423 d->cur_cell=next;
424 }
425
426 }
427
428 mp_msg(MSGT_DVD,MSGL_V, "DVD Seek! lba=0x%X cell=%d packs: 0x%X-0x%X \n",
429 d->cur_pack,d->cur_cell,d->cur_pgc->cell_playback[ d->cur_cell ].first_sector,d->cell_last_pack);
430
431 // if we're in interleaved multi-angle cell, find the right angle chain!
432 // (read Navi block, and use the seamless angle jump table)
433 d->angle_seek=1;
434
435 }
436
437 #endif