comparison ogg2.c @ 1185:13dc486b272b libavformat

Try to find out correct start time to make seeking faster and add some extra checks to make sure the seeking function will not hang forever.
author reimar
date Sun, 23 Jul 2006 18:19:33 +0000
parents d18cc9a1fd02
children d5d90b6f384c
comparison
equal deleted inserted replaced
1184:5a743f6f836a 1185:13dc486b272b
199 st = av_new_stream (s, idx); 199 st = av_new_stream (s, idx);
200 if (!st) 200 if (!st)
201 return AVERROR_NOMEM; 201 return AVERROR_NOMEM;
202 202
203 av_set_pts_info(st, 64, 1, 1000000); 203 av_set_pts_info(st, 64, 1, 1000000);
204 st->start_time = 0;
205 204
206 return idx; 205 return idx;
207 } 206 }
208 207
209 static int 208 static int
493 ogg_gptopts (s, idx, ogg->streams[idx].granule); 492 ogg_gptopts (s, idx, ogg->streams[idx].granule);
494 } 493 }
495 494
496 ogg->size = size; 495 ogg->size = size;
497 ogg_restore (s, 0); 496 ogg_restore (s, 0);
497 ogg_save (s);
498 while (ogg_read_page (s, &i)) {
499 if (i == idx && ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0)
500 break;
501 }
502 if (i == idx) {
503 s->streams[idx]->start_time = ogg_gptopts (s, idx, ogg->streams[idx].granule);
504 s->streams[idx]->duration -= s->streams[idx]->start_time;
505 }
506 ogg_restore (s, 0);
498 507
499 return 0; 508 return 0;
500 } 509 }
501 510
502 511
570 { 579 {
571 AVStream *st = s->streams[stream_index]; 580 AVStream *st = s->streams[stream_index];
572 ogg_t *ogg = s->priv_data; 581 ogg_t *ogg = s->priv_data;
573 ByteIOContext *bc = &s->pb; 582 ByteIOContext *bc = &s->pb;
574 uint64_t min = 0, max = ogg->size; 583 uint64_t min = 0, max = ogg->size;
575 uint64_t tmin = 0, tmax = st->duration; 584 uint64_t tmin = st->start_time, tmax = st->start_time + st->duration;
576 int64_t pts = AV_NOPTS_VALUE; 585 int64_t pts = AV_NOPTS_VALUE;
577 586
578 ogg_save (s); 587 ogg_save (s);
579 588
580 while (min <= max){ 589 if ((uint64_t)target_ts < tmin || target_ts < 0)
590 target_ts = tmin;
591 while (min <= max && tmin < tmax){
581 uint64_t p = min + (max - min) * (target_ts - tmin) / (tmax - tmin); 592 uint64_t p = min + (max - min) * (target_ts - tmin) / (tmax - tmin);
582 int i = -1; 593 int i = -1;
583 594
584 url_fseek (bc, p, SEEK_SET); 595 url_fseek (bc, p, SEEK_SET);
585 596
597 608
598 if (ABS (pts - target_ts) * st->time_base.num < st->time_base.den) 609 if (ABS (pts - target_ts) * st->time_base.num < st->time_base.den)
599 break; 610 break;
600 611
601 if (pts > target_ts){ 612 if (pts > target_ts){
613 if (max == p && tmax == pts) {
614 // probably our tmin is wrong, causing us to always end up too late in the file
615 tmin = (target_ts + tmin + 1) / 2;
616 if (tmin == target_ts) {
617 url_fseek(bc, min, SEEK_SET);
618 break;
619 }
620 }
602 max = p; 621 max = p;
603 tmax = pts; 622 tmax = pts;
604 }else{ 623 }else{
624 if (min == p && tmin == pts) {
625 // probably our tmax is wrong, causing us to always end up too early in the file
626 tmax = (target_ts + tmax) / 2;
627 if (tmax == target_ts) {
628 url_fseek(bc, max, SEEK_SET);
629 break;
630 }
631 }
605 min = p; 632 min = p;
606 tmin = pts; 633 tmin = pts;
607 } 634 }
608 } 635 }
609 636
613 }else{ 640 }else{
614 ogg_restore (s, 0); 641 ogg_restore (s, 0);
615 pts = AV_NOPTS_VALUE; 642 pts = AV_NOPTS_VALUE;
616 } 643 }
617 644
618 return pts; 645 av_update_cur_dts(s, st, pts);
646 return 0;
619 647
620 #if 0 648 #if 0
621 //later... 649 //later...
622 int64_t pos; 650 int64_t pos;
623 if (av_seek_frame_binary (s, stream_index, target_ts, flags) < 0) 651 if (av_seek_frame_binary (s, stream_index, target_ts, flags) < 0)