changeset 2544:fd48f0d813b6

using keyframe info for seeking
author arpi
date Mon, 29 Oct 2001 21:55:07 +0000
parents a28b5e54f5a6
children 89e3bf3c0c4e
files libmpdemux/demux_mov.c
diffstat 1 files changed, 20 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libmpdemux/demux_mov.c	Mon Oct 29 21:25:10 2001 +0000
+++ b/libmpdemux/demux_mov.c	Mon Oct 29 21:55:07 2001 +0000
@@ -71,6 +71,8 @@
     mov_chunkmap_t* chunkmap;
     int durmap_size;
     mov_durmap_t* durmap;
+    int keyframes_size;
+    unsigned int* keyframes;
 } mov_track_t;
 
 void mov_build_index(mov_track_t* trak){
@@ -372,15 +374,12 @@
 		int ver = (temp << 24);
 		int flags = (temp << 16)|(temp<<8)|temp;
 		int i;
-	
 		mp_msg(MSGT_DEMUX, MSGL_V,"MOV: %*sSyncing samples (keyframes) table! (%d entries) (ver:%d,flags:%ld)\n",
 		    level, "",entries, ver, flags);
-#if 0
-		for (i=0;i<entries;i++)
-		{
-		    printf("entry#%d: %ld\n", i, stream_read_dword(demuxer->stream));
-		}
-#endif
+		trak->keyframes_size=entries;
+		trak->keyframes=malloc(sizeof(unsigned int)*entries);
+		for (i=0;i<entries;i++) trak->keyframes[i]=stream_read_dword(demuxer->stream)-1;
+//		for (i=0;i<entries;i++) printf("%3d: %d\n",i,trak->keyframes[i]);
 		break;
 	    }
 	    case MOV_FOURCC('m','d','i','a'): {
@@ -751,8 +750,20 @@
     unsigned int ipts=pts;
 //    printf("MOV track seek - sample: %d  \n",ipts);
     if(!(flags&1)) ipts+=trak->samples[trak->pos].pts;
-    trak->pos=0;
-    while(trak->pos<trak->samples_size && trak->samples[trak->pos].pts<ipts) ++trak->pos;
+    for(trak->pos=0;trak->pos<trak->samples_size;++trak->pos){
+	if(trak->samples[trak->pos].pts>=ipts) break; // found it!
+    }
+    if(trak->keyframes_size){
+	// find nearest keyframe
+	int i;
+	for(i=0;i<trak->keyframes_size;i++){
+	    if(trak->keyframes[i]>=trak->pos) break;
+	}
+	if(i>0 && 
+	  (trak->keyframes[i]-trak->pos) > (trak->pos-trak->keyframes[i-1])) --i;
+	trak->pos=trak->keyframes[i];
+//	printf("nearest keyframe: %d  \n",trak->pos);
+    }
     pts=(float)trak->samples[trak->pos].pts/(float)trak->timescale;
 }