Fix segfault if an 'strf' chunk couldn't be found in avi
[mplayer/glamo.git] / libmpdemux / demuxer.c
blob6db07b12413deae079d7dc73aec9baea448535a0
1 //=================== DEMUXER v2.5 =========================
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
11 #include "config.h"
12 #include "mp_msg.h"
13 #include "help_mp.h"
14 #include "m_config.h"
16 #include "libvo/fastmemcpy.h"
18 #include "stream/stream.h"
19 #include "demuxer.h"
20 #include "stheader.h"
21 #include "mf.h"
23 #include "libaf/af_format.h"
25 extern void resync_video_stream(sh_video_t *sh_video);
26 extern void resync_audio_stream(sh_audio_t *sh_audio);
28 // Demuxer list
29 extern demuxer_desc_t demuxer_desc_rawaudio;
30 extern demuxer_desc_t demuxer_desc_rawvideo;
31 extern demuxer_desc_t demuxer_desc_tv;
32 extern demuxer_desc_t demuxer_desc_mf;
33 extern demuxer_desc_t demuxer_desc_avi;
34 extern demuxer_desc_t demuxer_desc_y4m;
35 extern demuxer_desc_t demuxer_desc_asf;
36 extern demuxer_desc_t demuxer_desc_nuv;
37 extern demuxer_desc_t demuxer_desc_real;
38 extern demuxer_desc_t demuxer_desc_smjpeg;
39 extern demuxer_desc_t demuxer_desc_matroska;
40 extern demuxer_desc_t demuxer_desc_realaudio;
41 extern demuxer_desc_t demuxer_desc_vqf;
42 extern demuxer_desc_t demuxer_desc_mov;
43 extern demuxer_desc_t demuxer_desc_vivo;
44 extern demuxer_desc_t demuxer_desc_fli;
45 extern demuxer_desc_t demuxer_desc_film;
46 extern demuxer_desc_t demuxer_desc_roq;
47 extern demuxer_desc_t demuxer_desc_gif;
48 extern demuxer_desc_t demuxer_desc_ogg;
49 extern demuxer_desc_t demuxer_desc_avs;
50 extern demuxer_desc_t demuxer_desc_pva;
51 extern demuxer_desc_t demuxer_desc_nsv;
52 extern demuxer_desc_t demuxer_desc_mpeg_ts;
53 extern demuxer_desc_t demuxer_desc_lmlm4;
54 extern demuxer_desc_t demuxer_desc_mpeg_ps;
55 extern demuxer_desc_t demuxer_desc_mpeg_pes;
56 extern demuxer_desc_t demuxer_desc_mpeg_es;
57 extern demuxer_desc_t demuxer_desc_mpeg_gxf;
58 extern demuxer_desc_t demuxer_desc_mpeg4_es;
59 extern demuxer_desc_t demuxer_desc_h264_es;
60 extern demuxer_desc_t demuxer_desc_rawdv;
61 extern demuxer_desc_t demuxer_desc_mpc;
62 extern demuxer_desc_t demuxer_desc_audio;
63 extern demuxer_desc_t demuxer_desc_xmms;
64 extern demuxer_desc_t demuxer_desc_mpeg_ty;
65 extern demuxer_desc_t demuxer_desc_rtp;
66 extern demuxer_desc_t demuxer_desc_lavf;
67 extern demuxer_desc_t demuxer_desc_lavf_preferred;
68 extern demuxer_desc_t demuxer_desc_aac;
69 extern demuxer_desc_t demuxer_desc_nut;
71 demuxer_desc_t* demuxer_list[] = {
72 &demuxer_desc_rawaudio,
73 &demuxer_desc_rawvideo,
74 #ifdef USE_TV
75 &demuxer_desc_tv,
76 #endif
77 &demuxer_desc_mf,
78 #ifdef USE_LIBAVFORMAT
79 &demuxer_desc_lavf_preferred,
80 #endif
81 &demuxer_desc_avi,
82 &demuxer_desc_y4m,
83 &demuxer_desc_asf,
84 &demuxer_desc_nsv,
85 &demuxer_desc_nuv,
86 &demuxer_desc_real,
87 &demuxer_desc_smjpeg,
88 &demuxer_desc_matroska,
89 &demuxer_desc_realaudio,
90 &demuxer_desc_vqf,
91 &demuxer_desc_mov,
92 &demuxer_desc_vivo,
93 &demuxer_desc_fli,
94 &demuxer_desc_film,
95 &demuxer_desc_roq,
96 #ifdef HAVE_GIF
97 &demuxer_desc_gif,
98 #endif
99 #ifdef HAVE_OGGVORBIS
100 &demuxer_desc_ogg,
101 #endif
102 #ifdef USE_WIN32DLL
103 &demuxer_desc_avs,
104 #endif
105 &demuxer_desc_pva,
106 &demuxer_desc_mpeg_ts,
107 &demuxer_desc_lmlm4,
108 &demuxer_desc_mpeg_ps,
109 &demuxer_desc_mpeg_pes,
110 &demuxer_desc_mpeg_es,
111 &demuxer_desc_mpeg_gxf,
112 &demuxer_desc_mpeg4_es,
113 &demuxer_desc_h264_es,
114 #ifdef MUSEPACK
115 &demuxer_desc_mpc,
116 #endif
117 &demuxer_desc_audio,
118 &demuxer_desc_mpeg_ty,
119 #ifdef STREAMING_LIVE555
120 &demuxer_desc_rtp,
121 #endif
122 #ifdef USE_LIBAVFORMAT
123 &demuxer_desc_lavf,
124 #endif
125 #ifdef HAVE_LIBDV095
126 &demuxer_desc_rawdv,
127 #endif
128 &demuxer_desc_aac,
129 #ifdef HAVE_LIBNUT
130 &demuxer_desc_nut,
131 #endif
132 #ifdef HAVE_XMMS
133 &demuxer_desc_xmms,
134 #endif
135 NULL
138 void free_demuxer_stream(demux_stream_t *ds){
139 ds_free_packs(ds);
140 free(ds);
143 demux_stream_t* new_demuxer_stream(struct demuxer_st *demuxer,int id){
144 demux_stream_t* ds=malloc(sizeof(demux_stream_t));
145 ds->buffer_pos=ds->buffer_size=0;
146 ds->buffer=NULL;
147 ds->pts=0;
148 ds->pts_bytes=0;
149 ds->eof=0;
150 ds->pos=0;
151 ds->dpos=0;
152 ds->pack_no=0;
153 //---------------
154 ds->packs=0;
155 ds->bytes=0;
156 ds->first=ds->last=ds->current=NULL;
157 ds->id=id;
158 ds->demuxer=demuxer;
159 //----------------
160 ds->asf_seq=-1;
161 ds->asf_packet=NULL;
162 //----------------
163 ds->ss_mul=ds->ss_div=0;
164 //----------------
165 ds->sh=NULL;
166 return ds;
171 * Get demuxer description structure for a given demuxer type
173 * @param file_format type of the demuxer
174 * @return structure for the demuxer, NULL if not found
176 static demuxer_desc_t* get_demuxer_desc_from_type(int file_format)
178 int i;
180 for (i = 0; demuxer_list[i]; i++)
181 if (file_format == demuxer_list[i]->type)
182 return demuxer_list[i];
184 return NULL;
188 demuxer_t* new_demuxer(stream_t *stream,int type,int a_id,int v_id,int s_id,char *filename){
189 demuxer_t *d=malloc(sizeof(demuxer_t));
190 memset(d,0,sizeof(demuxer_t));
191 d->stream=stream;
192 d->stream_pts = MP_NOPTS_VALUE;
193 d->movi_start=stream->start_pos;
194 d->movi_end=stream->end_pos;
195 d->seekable=1;
196 d->synced=0;
197 d->filepos=0;
198 d->audio=new_demuxer_stream(d,a_id);
199 d->video=new_demuxer_stream(d,v_id);
200 d->sub=new_demuxer_stream(d,s_id);
201 d->type=type;
202 if(type)
203 if (!(d->desc = get_demuxer_desc_from_type(type)))
204 mp_msg(MSGT_DEMUXER,MSGL_ERR,"BUG! Invalid demuxer type in new_demuxer(), big troubles ahead.");
205 if(filename) // Filename hack for avs_check_file
206 d->filename=strdup(filename);
207 stream_reset(stream);
208 stream_seek(stream,stream->start_pos);
209 return d;
212 sh_sub_t *new_sh_sub_sid(demuxer_t *demuxer, int id, int sid) {
213 if (id > MAX_S_STREAMS - 1 || id < 0) {
214 mp_msg(MSGT_DEMUXER,MSGL_WARN,"Requested sub stream id overflow (%d > %d)\n",
215 id, MAX_S_STREAMS);
216 return NULL;
218 if (demuxer->s_streams[id])
219 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Sub stream %i redefined\n", id);
220 else {
221 sh_sub_t *sh = calloc(1, sizeof(sh_sub_t));
222 demuxer->s_streams[id] = sh;
223 sh->sid = sid;
224 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sid);
226 return demuxer->s_streams[id];
229 void free_sh_sub(sh_sub_t *sh) {
230 mp_msg(MSGT_DEMUXER, MSGL_DBG2, "DEMUXER: freeing sh_sub at %p\n", sh);
231 free(sh);
234 sh_audio_t* new_sh_audio_aid(demuxer_t *demuxer,int id,int aid){
235 if(id > MAX_A_STREAMS-1 || id < 0)
237 mp_msg(MSGT_DEMUXER,MSGL_WARN,"Requested audio stream id overflow (%d > %d)\n",
238 id, MAX_A_STREAMS);
239 return NULL;
241 if(demuxer->a_streams[id]){
242 mp_msg(MSGT_DEMUXER,MSGL_WARN,MSGTR_AudioStreamRedefined,id);
243 } else {
244 sh_audio_t *sh;
245 mp_msg(MSGT_DEMUXER,MSGL_V,MSGTR_FoundAudioStream,id);
246 demuxer->a_streams[id]=calloc(1, sizeof(sh_audio_t));
247 sh = demuxer->a_streams[id];
248 // set some defaults
249 sh->samplesize=2;
250 sh->sample_format=AF_FORMAT_S16_NE;
251 sh->audio_out_minsize=8192;/* default size, maybe not enough for Win32/ACM*/
252 sh->pts=MP_NOPTS_VALUE;
253 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", aid);
255 ((sh_audio_t *)demuxer->a_streams[id])->aid = aid;
256 return demuxer->a_streams[id];
259 void free_sh_audio(demuxer_t *demuxer, int id) {
260 sh_audio_t *sh = demuxer->a_streams[id];
261 demuxer->a_streams[id] = NULL;
262 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"DEMUXER: freeing sh_audio at %p\n",sh);
263 if(sh->wf) free(sh->wf);
264 free(sh);
267 sh_video_t* new_sh_video_vid(demuxer_t *demuxer,int id,int vid){
268 if(id > MAX_V_STREAMS-1 || id < 0)
270 mp_msg(MSGT_DEMUXER,MSGL_WARN,"Requested video stream id overflow (%d > %d)\n",
271 id, MAX_V_STREAMS);
272 return NULL;
274 if(demuxer->v_streams[id]){
275 mp_msg(MSGT_DEMUXER,MSGL_WARN,MSGTR_VideoStreamRedefined,id);
276 } else {
277 mp_msg(MSGT_DEMUXER,MSGL_V,MSGTR_FoundVideoStream,id);
278 demuxer->v_streams[id]=calloc(1, sizeof(sh_video_t));
279 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VIDEO_ID=%d\n", vid);
281 ((sh_video_t *)demuxer->v_streams[id])->vid = vid;
282 return demuxer->v_streams[id];
285 void free_sh_video(sh_video_t* sh){
286 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"DEMUXER: freeing sh_video at %p\n",sh);
287 if(sh->bih) free(sh->bih);
288 free(sh);
291 void free_demuxer(demuxer_t *demuxer){
292 int i;
293 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"DEMUXER: freeing demuxer at %p\n",demuxer);
294 if(demuxer->desc->close)
295 demuxer->desc->close(demuxer);
296 // Very ugly hack to make it behave like old implementation
297 if (demuxer->desc->type == DEMUXER_TYPE_DEMUXERS)
298 goto skip_streamfree;
299 // free streams:
300 for(i = 0; i < MAX_A_STREAMS; i++)
301 if(demuxer->a_streams[i]) free_sh_audio(demuxer, i);
302 for(i = 0; i < MAX_V_STREAMS; i++)
303 if(demuxer->v_streams[i]) free_sh_video(demuxer->v_streams[i]);
304 for(i = 0; i < MAX_S_STREAMS; i++)
305 if(demuxer->s_streams[i]) free_sh_sub(demuxer->s_streams[i]);
306 // free demuxers:
307 free_demuxer_stream(demuxer->audio);
308 free_demuxer_stream(demuxer->video);
309 free_demuxer_stream(demuxer->sub);
310 skip_streamfree:
311 if(demuxer->info) {
312 for(i=0;demuxer->info[i] != NULL; i++)
313 free(demuxer->info[i]);
314 free(demuxer->info);
316 if(demuxer->filename)
317 free(demuxer->filename);
318 if (demuxer->chapters) {
319 for (i=0; i<demuxer->num_chapters; i++)
320 if (demuxer->chapters[i].name)
321 free(demuxer->chapters[i].name);
322 free(demuxer->chapters);
324 free(demuxer);
328 void ds_add_packet(demux_stream_t *ds,demux_packet_t* dp){
329 // demux_packet_t* dp=new_demux_packet(len);
330 // stream_read(stream,dp->buffer,len);
331 // dp->pts=pts; //(float)pts/90000.0f;
332 // dp->pos=pos;
333 // append packet to DS stream:
334 ++ds->packs;
335 ds->bytes+=dp->len;
336 if(ds->last){
337 // next packet in stream
338 ds->last->next=dp;
339 ds->last=dp;
340 } else {
341 // first packet in stream
342 ds->first=ds->last=dp;
344 mp_dbg(MSGT_DEMUXER,MSGL_DBG2,"DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%u [packs: A=%d V=%d]\n",
345 (ds==ds->demuxer->audio)?"d_audio":"d_video",
346 dp->len,dp->pts,(unsigned int)dp->pos,ds->demuxer->audio->packs,ds->demuxer->video->packs);
349 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len, double pts, off_t pos, int flags) {
350 demux_packet_t* dp=new_demux_packet(len);
351 len = stream_read(stream,dp->buffer,len);
352 resize_demux_packet(dp, len);
353 dp->pts=pts; //(float)pts/90000.0f;
354 dp->pos=pos;
355 dp->flags=flags;
356 // append packet to DS stream:
357 ds_add_packet(ds,dp);
360 // return value:
361 // 0 = EOF or no stream found or invalid type
362 // 1 = successfully read a packet
364 int demux_fill_buffer(demuxer_t *demux,demux_stream_t *ds){
365 // Note: parameter 'ds' can be NULL!
366 // printf("demux->type=%d\n",demux->type);
367 return demux->desc->fill_buffer(demux, ds);
370 // return value:
371 // 0 = EOF
372 // 1 = succesfull
373 int ds_fill_buffer(demux_stream_t *ds){
374 demuxer_t *demux=ds->demuxer;
375 if(ds->current) free_demux_packet(ds->current);
376 if( mp_msg_test(MSGT_DEMUXER,MSGL_DBG3) ){
377 if(ds==demux->audio) mp_dbg(MSGT_DEMUXER,MSGL_DBG3,"ds_fill_buffer(d_audio) called\n");else
378 if(ds==demux->video) mp_dbg(MSGT_DEMUXER,MSGL_DBG3,"ds_fill_buffer(d_video) called\n");else
379 if(ds==demux->sub) mp_dbg(MSGT_DEMUXER,MSGL_DBG3,"ds_fill_buffer(d_sub) called\n");else
380 mp_dbg(MSGT_DEMUXER,MSGL_DBG3,"ds_fill_buffer(unknown 0x%X) called\n",(unsigned int)ds);
382 while(1){
383 if(ds->packs){
384 demux_packet_t *p=ds->first;
385 // copy useful data:
386 ds->buffer=p->buffer;
387 ds->buffer_pos=0;
388 ds->buffer_size=p->len;
389 ds->pos=p->pos;
390 ds->dpos+=p->len; // !!!
391 ++ds->pack_no;
392 if (p->pts != (correct_pts ? MP_NOPTS_VALUE : 0)) {
393 ds->pts=p->pts;
394 ds->pts_bytes=0;
396 ds->pts_bytes+=p->len; // !!!
397 if(p->stream_pts != MP_NOPTS_VALUE) demux->stream_pts=p->stream_pts;
398 ds->flags=p->flags;
399 // unlink packet:
400 ds->bytes-=p->len;
401 ds->current=p;
402 ds->first=p->next;
403 if(!ds->first) ds->last=NULL;
404 --ds->packs;
405 return 1; //ds->buffer_size;
407 if(demux->audio->packs>=MAX_PACKS || demux->audio->bytes>=MAX_PACK_BYTES){
408 mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_TooManyAudioInBuffer,demux->audio->packs,demux->audio->bytes);
409 mp_msg(MSGT_DEMUXER,MSGL_HINT,MSGTR_MaybeNI);
410 break;
412 if(demux->video->packs>=MAX_PACKS || demux->video->bytes>=MAX_PACK_BYTES){
413 mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_TooManyVideoInBuffer,demux->video->packs,demux->video->bytes);
414 mp_msg(MSGT_DEMUXER,MSGL_HINT,MSGTR_MaybeNI);
415 break;
417 if(!demux_fill_buffer(demux,ds)){
418 mp_dbg(MSGT_DEMUXER,MSGL_DBG2,"ds_fill_buffer()->demux_fill_buffer() failed\n");
419 break; // EOF
422 ds->buffer_pos=ds->buffer_size=0;
423 ds->buffer=NULL;
424 ds->current=NULL;
425 mp_msg(MSGT_DEMUXER,MSGL_V,"ds_fill_buffer: EOF reached (stream: %s) \n",ds==demux->audio?"audio":"video");
426 ds->eof=1;
427 return 0;
430 int demux_read_data(demux_stream_t *ds,unsigned char* mem,int len){
431 int x;
432 int bytes=0;
433 while(len>0){
434 x=ds->buffer_size-ds->buffer_pos;
435 if(x==0){
436 if(!ds_fill_buffer(ds)) return bytes;
437 } else {
438 if(x>len) x=len;
439 if(mem) fast_memcpy(mem+bytes,&ds->buffer[ds->buffer_pos],x);
440 bytes+=x;len-=x;ds->buffer_pos+=x;
443 return bytes;
446 int demux_read_data_pack(demux_stream_t *ds,unsigned char* mem,int len){
447 int x;
448 int bytes=0;
449 while(len>0){
450 x=ds->buffer_size-ds->buffer_pos;
451 if(x==0){
452 if(!ds_fill_buffer(ds)) return bytes;
453 } else {
454 if(x>len) x=len;
455 if(mem) fast_memcpy(mem+bytes,&ds->buffer[ds->buffer_pos],x);
456 bytes+=x;len-=x;ds->buffer_pos+=x;
457 return bytes; // stop at end of package! (for correct timestamping)
460 return bytes;
464 * \brief read data until the given 3-byte pattern is encountered, up to maxlen
465 * \param mem memory to read data into, may be NULL to discard data
466 * \param maxlen maximum number of bytes to read
467 * \param read number of bytes actually read
468 * \param pattern pattern to search for (lowest 8 bits are ignored)
469 * \return whether pattern was found
471 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
472 int *read, uint32_t pattern) {
473 register uint32_t head = 0xffffff00;
474 register uint32_t pat = pattern & 0xffffff00;
475 int total_len = 0;
476 do {
477 register unsigned char *ds_buf = &ds->buffer[ds->buffer_size];
478 int len = ds->buffer_size - ds->buffer_pos;
479 register long pos = -len;
480 if (unlikely(pos >= 0)) { // buffer is empty
481 ds_fill_buffer(ds);
482 continue;
484 do {
485 head |= ds_buf[pos];
486 head <<= 8;
487 } while (++pos && head != pat);
488 len += pos;
489 if (total_len + len > maxlen)
490 len = maxlen - total_len;
491 len = demux_read_data(ds, mem ? &mem[total_len] : NULL, len);
492 total_len += len;
493 } while ((head != pat || total_len < 3) && total_len < maxlen && !ds->eof);
494 if (read)
495 *read = total_len;
496 return total_len >= 3 && head == pat;
499 void ds_free_packs(demux_stream_t *ds){
500 demux_packet_t *dp=ds->first;
501 while(dp){
502 demux_packet_t *dn=dp->next;
503 free_demux_packet(dp);
504 dp=dn;
506 if(ds->asf_packet){
507 // free unfinished .asf fragments:
508 free(ds->asf_packet->buffer);
509 free(ds->asf_packet);
510 ds->asf_packet=NULL;
512 ds->first=ds->last=NULL;
513 ds->packs=0; // !!!!!
514 ds->bytes=0;
515 if(ds->current) free_demux_packet(ds->current);
516 ds->current=NULL;
517 ds->buffer=NULL;
518 ds->buffer_pos=ds->buffer_size;
519 ds->pts=0; ds->pts_bytes=0;
522 int ds_get_packet(demux_stream_t *ds,unsigned char **start){
523 while(1){
524 int len;
525 if(ds->buffer_pos>=ds->buffer_size){
526 if(!ds_fill_buffer(ds)){
527 // EOF
528 *start = NULL;
529 return -1;
532 len=ds->buffer_size-ds->buffer_pos;
533 *start = &ds->buffer[ds->buffer_pos];
534 ds->buffer_pos+=len;
535 return len;
539 int ds_get_packet_pts(demux_stream_t *ds,unsigned char **start, double *pts)
541 int len;
542 *pts = MP_NOPTS_VALUE;
543 if(ds->buffer_pos>=ds->buffer_size){
544 if (!ds_fill_buffer(ds)) {
545 // EOF
546 *start = NULL;
547 return -1;
550 // Should use MP_NOPTS_VALUE for "unknown pts" in the packets too
551 // Return pts unless this read starts from the middle of a packet
552 if (!ds->buffer_pos && (correct_pts || ds->current->pts))
553 *pts = ds->current->pts;
554 len=ds->buffer_size-ds->buffer_pos;
555 *start = &ds->buffer[ds->buffer_pos];
556 ds->buffer_pos+=len;
557 return len;
560 int ds_get_packet_sub(demux_stream_t *ds,unsigned char **start){
561 while(1){
562 int len;
563 if(ds->buffer_pos>=ds->buffer_size){
564 *start = NULL;
565 if(!ds->packs) return -1; // no sub
566 if(!ds_fill_buffer(ds)) return -1; // EOF
568 len=ds->buffer_size-ds->buffer_pos;
569 *start = &ds->buffer[ds->buffer_pos];
570 ds->buffer_pos+=len;
571 return len;
575 double ds_get_next_pts(demux_stream_t *ds)
577 demuxer_t* demux = ds->demuxer;
578 while(!ds->first) {
579 if(demux->audio->packs>=MAX_PACKS || demux->audio->bytes>=MAX_PACK_BYTES){
580 mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_TooManyAudioInBuffer,demux->audio->packs,demux->audio->bytes);
581 mp_msg(MSGT_DEMUXER,MSGL_HINT,MSGTR_MaybeNI);
582 return MP_NOPTS_VALUE;
584 if(demux->video->packs>=MAX_PACKS || demux->video->bytes>=MAX_PACK_BYTES){
585 mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_TooManyVideoInBuffer,demux->video->packs,demux->video->bytes);
586 mp_msg(MSGT_DEMUXER,MSGL_HINT,MSGTR_MaybeNI);
587 return MP_NOPTS_VALUE;
589 if(!demux_fill_buffer(demux,ds))
590 return MP_NOPTS_VALUE;
592 return ds->first->pts;
595 // ====================================================================
597 // commandline options, flags:
598 extern int force_ni;
599 extern int pts_from_bps;
601 //extern int audio_id;
602 //extern int video_id;
603 //extern int dvdsub_id;
605 void demuxer_help(void)
607 int i;
609 mp_msg(MSGT_DEMUXER, MSGL_INFO, "Available demuxers:\n");
610 mp_msg(MSGT_DEMUXER, MSGL_INFO, " demuxer: type info: (comment)\n");
611 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DEMUXERS\n");
612 for (i = 0; demuxer_list[i]; i++) {
613 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Don't display special demuxers
614 continue;
615 if (demuxer_list[i]->comment && strlen(demuxer_list[i]->comment))
616 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s (%s)\n",
617 demuxer_list[i]->name, demuxer_list[i]->type, demuxer_list[i]->info, demuxer_list[i]->comment);
618 else
619 mp_msg(MSGT_DEMUXER, MSGL_INFO, "%10s %2d %s\n",
620 demuxer_list[i]->name, demuxer_list[i]->type, demuxer_list[i]->info);
626 * Get demuxer type for a given demuxer name
628 * @param demuxer_name string with demuxer name of demuxer number
629 * @param force will be set if demuxer should be forced.
630 * May be NULL.
631 * @return DEMUXER_TYPE_xxx, -1 if error or not found
633 int get_demuxer_type_from_name(char *demuxer_name, int *force)
635 int i;
636 long type_int;
637 char *endptr;
639 if (!demuxer_name || !demuxer_name[0])
640 return DEMUXER_TYPE_UNKNOWN;
641 if (force) *force = demuxer_name[0] == '+';
642 if (demuxer_name[0] == '+')
643 demuxer_name = &demuxer_name[1];
644 for (i = 0; demuxer_list[i]; i++) {
645 if (demuxer_list[i]->type > DEMUXER_TYPE_MAX) // Can't select special demuxers from commandline
646 continue;
647 if (strcmp(demuxer_name, demuxer_list[i]->name) == 0)
648 return demuxer_list[i]->type;
651 // No match found, try to parse name as an integer (demuxer number)
652 type_int = strtol(demuxer_name, &endptr, 0);
653 if (*endptr) // Conversion failed
654 return -1;
655 if ((type_int > 0) && (type_int <= DEMUXER_TYPE_MAX))
656 return (int)type_int;
658 return -1;
661 int extension_parsing=1; // 0=off 1=mixed (used only for unstable formats)
663 int correct_pts=0;
666 NOTE : Several demuxers may be opened at the same time so
667 demuxers should NEVER rely on an external var to enable them
668 self. If a demuxer can't do any autodection it should only use
669 file_format. The user can explictly set file_format with the -demuxer
670 option so there is really no need for another extra var.
671 For conivence an option can be added to set file_format directly
672 to the right type (ex: rawaudio,rawvideo).
673 Also the stream can override the file_format so a demuxer wich rely
674 on a special stream type can set file_format at the stream level
675 (ex: tv,mf).
678 static demuxer_t* demux_open_stream(stream_t *stream, int file_format,
679 int force, int audio_id, int video_id, int dvdsub_id,
680 char* filename) {
682 //int file_format=(*file_format_ptr);
684 demuxer_t *demuxer=NULL;
686 sh_video_t *sh_video=NULL;
688 demuxer_desc_t *demuxer_desc;
689 int fformat;
690 int i;
692 //printf("demux_open(%p,%d,%d,%d,%d) \n",stream,file_format,audio_id,video_id,dvdsub_id);
694 // If somebody requested a demuxer check it
695 if (file_format) {
696 if ((demuxer_desc = get_demuxer_desc_from_type(file_format))) {
697 demuxer = new_demuxer(stream,demuxer_desc->type,audio_id,video_id,dvdsub_id,filename);
698 if (demuxer_desc->check_file)
699 fformat = demuxer_desc->check_file(demuxer);
700 if (force || !demuxer_desc->check_file)
701 fformat = demuxer_desc->type;
702 if (fformat != 0) {
703 if (fformat == demuxer_desc->type) {
704 demuxer_t *demux2 = demuxer;
705 // Move messages to demuxer detection code?
706 mp_msg(MSGT_DEMUXER, MSGL_INFO, MSGTR_Detected_XXX_FileFormat, demuxer_desc->shortdesc);
707 file_format = demuxer_desc->type = fformat;
708 if (!demuxer->desc->open || (demux2 = demuxer->desc->open(demuxer))) {
709 demuxer = demux2;
710 goto dmx_open;
712 } else {
713 // Format changed after check, recurse
714 free_demuxer(demuxer);
715 return demux_open_stream(stream, fformat, force,
716 audio_id, video_id, dvdsub_id, filename);
719 // Check failed for forced demuxer, quit
720 free_demuxer(demuxer);
721 return NULL;
725 // Test demuxers with safe file checks
726 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
727 if (demuxer_desc->safe_check) {
728 demuxer = new_demuxer(stream,demuxer_desc->type,audio_id,video_id,dvdsub_id,filename);
729 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
730 if (fformat == demuxer_desc->type) {
731 demuxer_t *demux2 = demuxer;
732 mp_msg(MSGT_DEMUXER, MSGL_INFO, MSGTR_Detected_XXX_FileFormat, demuxer_desc->shortdesc);
733 file_format = fformat;
734 if (!demuxer->desc->open || (demux2 = demuxer->desc->open(demuxer))) {
735 demuxer = demux2;
736 goto dmx_open;
738 } else {
739 if (fformat == DEMUXER_TYPE_PLAYLIST)
740 return demuxer; // handled in mplayer.c
741 // Format changed after check, recurse
742 free_demuxer(demuxer);
743 demuxer=demux_open_stream(stream, fformat, force,
744 audio_id, video_id, dvdsub_id, filename);
745 if(demuxer) return demuxer; // done!
746 file_format = DEMUXER_TYPE_UNKNOWN;
749 free_demuxer(demuxer);
750 demuxer = NULL;
754 // If no forced demuxer perform file extension based detection
755 // Ok. We're over the stable detectable fileformats, the next ones are a bit
756 // fuzzy. So by default (extension_parsing==1) try extension-based detection
757 // first:
758 if(file_format==DEMUXER_TYPE_UNKNOWN && filename && extension_parsing==1){
759 file_format=demuxer_type_by_filename(filename);
760 if(file_format!=DEMUXER_TYPE_UNKNOWN){
761 // we like recursion :)
762 demuxer=demux_open_stream(stream, file_format, force,
763 audio_id, video_id, dvdsub_id, filename);
764 if(demuxer) return demuxer; // done!
765 file_format=DEMUXER_TYPE_UNKNOWN; // continue fuzzy guessing...
766 mp_msg(MSGT_DEMUXER,MSGL_V,"demuxer: continue fuzzy content-based format guessing...\n");
770 // Try detection for all other demuxers
771 for (i = 0; (demuxer_desc = demuxer_list[i]); i++) {
772 if (!demuxer_desc->safe_check && demuxer_desc->check_file) {
773 demuxer = new_demuxer(stream,demuxer_desc->type,audio_id,video_id,dvdsub_id,filename);
774 if ((fformat = demuxer_desc->check_file(demuxer)) != 0) {
775 if (fformat == demuxer_desc->type) {
776 demuxer_t *demux2 = demuxer;
777 mp_msg(MSGT_DEMUXER, MSGL_INFO, MSGTR_Detected_XXX_FileFormat, demuxer_desc->shortdesc);
778 file_format = fformat;
779 if (!demuxer->desc->open || (demux2 = demuxer->desc->open(demuxer))) {
780 demuxer = demux2;
781 goto dmx_open;
783 } else {
784 if (fformat == DEMUXER_TYPE_PLAYLIST)
785 return demuxer; // handled in mplayer.c
786 // Format changed after check, recurse
787 free_demuxer(demuxer);
788 demuxer=demux_open_stream(stream, fformat, force,
789 audio_id, video_id, dvdsub_id, filename);
790 if(demuxer) return demuxer; // done!
791 file_format = DEMUXER_TYPE_UNKNOWN;
794 free_demuxer(demuxer);
795 demuxer = NULL;
799 return NULL;
800 //====== File format recognized, set up these for compatibility: =========
801 dmx_open:
803 demuxer->file_format=file_format;
805 if ((sh_video=demuxer->video->sh) && sh_video->bih){
806 int biComp=le2me_32(sh_video->bih->biCompression);
807 mp_msg(MSGT_DEMUX,MSGL_INFO,"VIDEO: [%.4s] %dx%d %dbpp %5.3f fps %5.1f kbps (%4.1f kbyte/s)\n",
808 (char *)&biComp,
809 sh_video->bih->biWidth,
810 sh_video->bih->biHeight,
811 sh_video->bih->biBitCount,
812 sh_video->fps,
813 sh_video->i_bps*0.008f,
814 sh_video->i_bps/1024.0f );
816 return demuxer;
819 char* audio_stream = NULL;
820 char* sub_stream = NULL;
821 int demuxer_type = 0; // used by rawaudio and rawvideo
822 int audio_stream_cache = 0;
824 char *demuxer_name = NULL; // parameter from -demuxer
825 char *audio_demuxer_name = NULL; // parameter from -audio-demuxer
826 char *sub_demuxer_name = NULL; // parameter from -sub-demuxer
828 extern int hr_mp3_seek;
830 extern float stream_cache_min_percent;
831 extern float stream_cache_seek_min_percent;
833 demuxer_t* demux_open(stream_t *vs,int file_format,int audio_id,int video_id,int dvdsub_id,char* filename){
834 stream_t *as = NULL,*ss = NULL;
835 demuxer_t *vd,*ad = NULL,*sd = NULL;
836 int afmt =DEMUXER_TYPE_UNKNOWN,sfmt = DEMUXER_TYPE_UNKNOWN ;
837 int audio_demuxer_type = 0, sub_demuxer_type = 0;
838 int demuxer_force = 0, audio_demuxer_force = 0,
839 sub_demuxer_force = 0;
841 if ((demuxer_type = get_demuxer_type_from_name(demuxer_name, &demuxer_force)) < 0) {
842 mp_msg(MSGT_DEMUXER,MSGL_ERR,"-demuxer %s does not exist.\n",demuxer_name);
844 if ((audio_demuxer_type = get_demuxer_type_from_name(audio_demuxer_name, &audio_demuxer_force)) < 0) {
845 mp_msg(MSGT_DEMUXER,MSGL_ERR,"-audio-demuxer %s does not exist.\n",audio_demuxer_name);
847 if ((sub_demuxer_type = get_demuxer_type_from_name(sub_demuxer_name, &sub_demuxer_force)) < 0) {
848 mp_msg(MSGT_DEMUXER,MSGL_ERR,"-sub-demuxer %s does not exist.\n",sub_demuxer_name);
851 if(audio_stream) {
852 as = open_stream(audio_stream,0,&afmt);
853 if(!as) {
854 mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_CannotOpenAudioStream,audio_stream);
855 return NULL;
857 if(audio_stream_cache) {
858 if(!stream_enable_cache(as,audio_stream_cache*1024,audio_stream_cache*1024*(stream_cache_min_percent / 100.0),
859 audio_stream_cache*1024*(stream_cache_seek_min_percent / 100.0))) {
860 free_stream(as);
861 mp_msg(MSGT_DEMUXER,MSGL_ERR,"Can't enable audio stream cache\n");
862 return NULL;
866 if(sub_stream) {
867 ss = open_stream(sub_stream,0,&sfmt);
868 if(!ss) {
869 mp_msg(MSGT_DEMUXER,MSGL_ERR,MSGTR_CannotOpenSubtitlesStream,sub_stream);
870 return NULL;
874 vd = demux_open_stream(vs, demuxer_type ? demuxer_type : file_format,
875 demuxer_force, audio_stream ? -2 : audio_id, video_id,
876 sub_stream ? -2 : dvdsub_id, filename);
877 if(!vd) {
878 if(as) free_stream(as);
879 if(ss) free_stream(ss);
880 return NULL;
882 if(as) {
883 ad = demux_open_stream(as, audio_demuxer_type ? audio_demuxer_type : afmt,
884 audio_demuxer_force, audio_id, -2, -2, audio_stream);
885 if(!ad) {
886 mp_msg(MSGT_DEMUXER,MSGL_WARN,MSGTR_OpeningAudioDemuxerFailed,audio_stream);
887 free_stream(as);
889 else if(ad->audio->sh && ((sh_audio_t*)ad->audio->sh)->format == 0x55) // MP3
890 hr_mp3_seek=1; // Enable high res seeking
892 if(ss) {
893 sd = demux_open_stream(ss, sub_demuxer_type ? sub_demuxer_type : sfmt,
894 sub_demuxer_force, -2, -2, dvdsub_id, sub_stream);
895 if(!sd) {
896 mp_msg(MSGT_DEMUXER,MSGL_WARN,MSGTR_OpeningSubtitlesDemuxerFailed,sub_stream);
897 free_stream(ss);
901 if(ad && sd)
902 return new_demuxers_demuxer(vd,ad,sd);
903 else if(ad)
904 return new_demuxers_demuxer(vd,ad,vd);
905 else if(sd)
906 return new_demuxers_demuxer(vd,vd,sd);
907 else
908 return vd;
912 int demux_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
913 demux_stream_t *d_audio=demuxer->audio;
914 demux_stream_t *d_video=demuxer->video;
915 sh_audio_t *sh_audio=d_audio->sh;
916 sh_video_t *sh_video=d_video->sh;
917 double tmp = 0;
918 double pts;
920 if(!demuxer->seekable){
921 if(demuxer->file_format==DEMUXER_TYPE_AVI)
922 mp_msg(MSGT_SEEK,MSGL_WARN,MSGTR_CantSeekRawAVI);
923 #ifdef USE_TV
924 else if (demuxer->file_format==DEMUXER_TYPE_TV)
925 mp_msg(MSGT_SEEK,MSGL_WARN,MSGTR_TVInputNotSeekable);
926 #endif
927 else
928 mp_msg(MSGT_SEEK,MSGL_WARN,MSGTR_CantSeekFile);
929 return 0;
932 // clear demux buffers:
933 if(sh_audio){ ds_free_packs(d_audio);sh_audio->a_buffer_len=0;}
934 ds_free_packs(d_video);
936 demuxer->stream->eof=0; // clear eof flag
937 demuxer->video->eof=0;
938 demuxer->audio->eof=0;
940 #if 0
941 if(sh_audio) sh_audio->timer=sh_video->timer;
942 #else
943 if(sh_video) sh_video->timer=0; // !!!!!!
944 #endif
946 if(flags & 1) // absolute seek
947 pts = 0.0f;
948 else {
949 if(demuxer->stream_pts == MP_NOPTS_VALUE)
950 goto dmx_seek;
951 pts = demuxer->stream_pts;
954 if(flags & 2) { // percent seek
955 if(stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &tmp) == STREAM_UNSUPORTED)
956 goto dmx_seek;
957 pts += tmp * rel_seek_secs;
958 } else
959 pts += rel_seek_secs;
961 if(stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts) != STREAM_UNSUPORTED) {
962 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
963 return 1;
966 dmx_seek:
967 if (demuxer->desc->seek)
968 demuxer->desc->seek(demuxer,rel_seek_secs,audio_delay,flags);
970 if (sh_audio) resync_audio_stream(sh_audio);
972 return 1;
975 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param)
977 char **info = demuxer->info;
978 int n = 0;
981 for(n = 0; info && info[2*n] != NULL; n++)
983 if(!strcasecmp(opt,info[2*n]))
985 mp_msg(MSGT_DEMUX, MSGL_INFO,MSGTR_DemuxerInfoChanged,opt,param);
986 free(info[2*n+1]);
987 info[2*n+1] = strdup(param);
988 return 0;
992 info = demuxer->info = (char**)realloc(info,(2*(n+2))*sizeof(char*));
993 info[2*n] = strdup(opt);
994 info[2*n+1] = strdup(param);
995 memset(&info[2*(n+1)],0,2*sizeof(char*));
997 return 1;
1000 int demux_info_print(demuxer_t *demuxer)
1002 char **info = demuxer->info;
1003 int n;
1005 if(!info)
1006 return 0;
1008 mp_msg(MSGT_DEMUX, MSGL_INFO,MSGTR_ClipInfo);
1009 for(n = 0; info[2*n] != NULL ; n++)
1011 mp_msg(MSGT_DEMUX, MSGL_INFO, " %s: %s\n",info[2*n],info[2*n+1]);
1012 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_NAME%d=%s\n", n, info[2*n]);
1013 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_VALUE%d=%s\n", n, info[2*n+1]);
1015 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CLIP_INFO_N=%d\n", n);
1017 return 0;
1020 char* demux_info_get(demuxer_t *demuxer, const char *opt) {
1021 int i;
1022 char **info = demuxer->info;
1024 for(i = 0; info && info[2*i] != NULL; i++) {
1025 if(!strcasecmp(opt,info[2*i]))
1026 return info[2*i+1];
1029 return NULL;
1032 int demux_control(demuxer_t *demuxer, int cmd, void *arg) {
1034 if (demuxer->desc->control)
1035 return demuxer->desc->control(demuxer,cmd,arg);
1037 return DEMUXER_CTRL_NOTIMPL;
1042 double demuxer_get_time_length(demuxer_t *demuxer){
1043 double get_time_ans;
1044 sh_video_t *sh_video = demuxer->video->sh;
1045 sh_audio_t *sh_audio = demuxer->audio->sh;
1046 // <= 0 means DEMUXER_CTRL_NOTIMPL or DEMUXER_CTRL_DONTKNOW
1047 if (demux_control(demuxer, DEMUXER_CTRL_GET_TIME_LENGTH,(void *)&get_time_ans)<=0) {
1048 if (sh_video && sh_video->i_bps && sh_audio && sh_audio->i_bps)
1049 get_time_ans = (double)(demuxer->movi_end-demuxer->movi_start)/(sh_video->i_bps+sh_audio->i_bps);
1050 else if (sh_video && sh_video->i_bps)
1051 get_time_ans = (double)(demuxer->movi_end-demuxer->movi_start)/sh_video->i_bps;
1052 else if (sh_audio && sh_audio->i_bps)
1053 get_time_ans = (double)(demuxer->movi_end-demuxer->movi_start)/sh_audio->i_bps;
1054 else
1055 get_time_ans=0;
1057 return get_time_ans;
1061 * \brief demuxer_get_current_time() returns the time of the current play in three possible ways:
1062 * either when the stream reader satisfies STREAM_CTRL_GET_CURRENT_TIME (e.g. dvd)
1063 * or using sh_video->pts when the former method fails
1064 * 0 otherwise
1065 * \return the current play time
1067 int demuxer_get_current_time(demuxer_t *demuxer){
1068 double get_time_ans = 0;
1069 sh_video_t *sh_video = demuxer->video->sh;
1070 if(demuxer->stream_pts != MP_NOPTS_VALUE)
1071 get_time_ans = demuxer->stream_pts;
1072 else if(sh_video) get_time_ans = sh_video->pts;
1073 return (int) get_time_ans;
1076 int demuxer_get_percent_pos(demuxer_t *demuxer){
1077 int ans = 0;
1078 int res = demux_control(demuxer, DEMUXER_CTRL_GET_PERCENT_POS, &ans);
1079 int len = (demuxer->movi_end - demuxer->movi_start) / 100;
1080 if (res <= 0) {
1081 if (len > 0)
1082 ans = (demuxer->filepos - demuxer->movi_start) / len;
1083 else
1084 ans = 0;
1086 if (ans < 0) ans = 0;
1087 if (ans > 100) ans = 100;
1088 return ans;
1091 int demuxer_switch_audio(demuxer_t *demuxer, int index){
1092 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_AUDIO, &index);
1093 if (res == DEMUXER_CTRL_NOTIMPL)
1094 index = demuxer->audio->id;
1095 return index;
1098 int demuxer_switch_video(demuxer_t *demuxer, int index){
1099 int res = demux_control(demuxer, DEMUXER_CTRL_SWITCH_VIDEO, &index);
1100 if (res == DEMUXER_CTRL_NOTIMPL)
1101 index = demuxer->video->id;
1102 return index;
1105 int demuxer_add_chapter(demuxer_t* demuxer, const char* name, uint64_t start, uint64_t end){
1106 if (demuxer->chapters == NULL)
1107 demuxer->chapters = malloc (32*sizeof(*demuxer->chapters));
1108 else if (!(demuxer->num_chapters % 32))
1109 demuxer->chapters = realloc (demuxer->chapters, (demuxer->num_chapters + 32) * sizeof(*demuxer->chapters));
1111 demuxer->chapters[demuxer->num_chapters].start = start;
1112 demuxer->chapters[demuxer->num_chapters].end = end;
1113 demuxer->chapters[demuxer->num_chapters].name = strdup(name);
1115 return demuxer->num_chapters ++;
1119 * \brief demuxer_seek_chapter() seeks to a chapter in two possible ways:
1120 * either using the demuxer->chapters structure set by the demuxer
1121 * or asking help to the stream layer (e.g. dvd)
1122 * \param chapter - chapter number wished - 0-based
1123 * \param mode 0: relative to current main pts, 1: absolute
1124 * \param seek_pts set by the function to the pts to seek to (if demuxer->chapters is set)
1125 * \param num_chapters number of chapters present (set by this function is param is not null)
1126 * \param chapter_name name of chapter found (set by this function is param is not null)
1127 * \return -1 on error, current chapter if successful
1130 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, int mode, float *seek_pts, int *num_chapters, char **chapter_name) {
1131 int ris;
1132 int current, total;
1133 sh_video_t *sh_video = demuxer->video->sh;
1134 sh_audio_t *sh_audio = demuxer->audio->sh;
1136 if (!demuxer->num_chapters || !demuxer->chapters) {
1137 if(!mode) {
1138 ris = stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_CHAPTER, &current);
1139 if(ris == STREAM_UNSUPORTED) return -1;
1140 chapter += current;
1143 if(demuxer->video->sh)
1144 ds_free_packs(demuxer->video);
1146 if(demuxer->audio->sh)
1147 ds_free_packs(demuxer->audio);
1149 if(demuxer->sub->id >= 0)
1150 ds_free_packs(demuxer->sub);
1152 ris = stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_CHAPTER, &chapter);
1153 if(ris != STREAM_UNSUPORTED)
1154 demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL);
1155 if(sh_video) {
1156 ds_fill_buffer(demuxer->video);
1157 resync_video_stream(sh_video);
1160 if(sh_audio) {
1161 ds_fill_buffer(demuxer->audio);
1162 resync_audio_stream(sh_audio);
1165 //exit status may be ok, but main() doesn't have to seek itself (because e.g. dvds depend on sectors, not on pts)
1166 *seek_pts = -1.0;
1168 if(num_chapters) {
1169 if(stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, num_chapters) == STREAM_UNSUPORTED)
1170 *num_chapters = 0;
1173 if(chapter_name) {
1174 char *tmp = malloc(16);
1175 *chapter_name = NULL;
1176 if(num_chapters && *num_chapters && tmp) {
1177 sprintf(tmp, " of %3d", *num_chapters);
1178 *chapter_name = tmp;
1182 return (ris != STREAM_UNSUPORTED ? chapter : -1);
1183 } else { //chapters structure is set in the demuxer
1184 total = demuxer->num_chapters;
1186 if (mode==1) { //absolute seeking
1187 current = chapter;
1188 } else { //relative seeking
1189 uint64_t now;
1190 now = (sh_video ? sh_video->pts : (sh_audio ? sh_audio->pts : 0.)) * 1000 + .5;
1192 for (current = total - 1; current >= 0; --current) {
1193 demux_chapter_t* chapter = demuxer->chapters + current;
1194 if (chapter->start <= now)
1195 break;
1197 current += chapter;
1200 if (current >= total)
1201 return -1;
1202 if (current < 0) current = 0;
1204 *seek_pts = demuxer->chapters[current].start / 1000.0;
1206 if(num_chapters)
1207 *num_chapters = demuxer->num_chapters;
1209 if(chapter_name) {
1210 if(demuxer->chapters[current].name)
1211 *chapter_name = strdup(demuxer->chapters[current].name);
1212 else *chapter_name = NULL;
1215 return current;