comparison utils.c @ 698:829c5c8e5cf2 libavformat

keyframe & non keyframe index fixes
author michael
date Sun, 13 Mar 2005 00:13:01 +0000
parents e2687b784c3a
children d79164865a7c
comparison
equal deleted inserted replaced
697:c7e1501cc306 698:829c5c8e5cf2
1022 if(!entries) 1022 if(!entries)
1023 return -1; 1023 return -1;
1024 1024
1025 st->index_entries= entries; 1025 st->index_entries= entries;
1026 1026
1027 index= av_index_search_timestamp(st, timestamp, 0); 1027 index= av_index_search_timestamp(st, timestamp, AVSEEK_FLAG_ANY);
1028 1028
1029 if(index<0){ 1029 if(index<0){
1030 index= st->nb_index_entries++; 1030 index= st->nb_index_entries++;
1031 ie= &entries[index]; 1031 ie= &entries[index];
1032 assert(index==0 || ie[-1].timestamp < timestamp); 1032 assert(index==0 || ie[-1].timestamp < timestamp);
1088 return 1; 1088 return 1;
1089 } 1089 }
1090 1090
1091 /** 1091 /**
1092 * gets the index for a specific timestamp. 1092 * gets the index for a specific timestamp.
1093 * @param backward if non zero then the returned index will correspond to 1093 * @param flags if AVSEEK_FLAG_BACKWARD then the returned index will correspond to
1094 * the timestamp which is <= the requested one, if backward is 0 1094 * the timestamp which is <= the requested one, if backward is 0
1095 * then it will be >= 1095 * then it will be >=
1096 * if AVSEEK_FLAG_ANY seek to any frame, only keyframes otherwise
1096 * @return < 0 if no such timestamp could be found 1097 * @return < 0 if no such timestamp could be found
1097 */ 1098 */
1098 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, 1099 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp,
1099 int backward) 1100 int flags)
1100 { 1101 {
1101 AVIndexEntry *entries= st->index_entries; 1102 AVIndexEntry *entries= st->index_entries;
1102 int nb_entries= st->nb_index_entries; 1103 int nb_entries= st->nb_index_entries;
1103 int a, b, m; 1104 int a, b, m;
1104 int64_t timestamp; 1105 int64_t timestamp;
1112 if(timestamp >= wanted_timestamp) 1113 if(timestamp >= wanted_timestamp)
1113 b = m; 1114 b = m;
1114 if(timestamp <= wanted_timestamp) 1115 if(timestamp <= wanted_timestamp)
1115 a = m; 1116 a = m;
1116 } 1117 }
1117 m= backward ? a : b; 1118 m= (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
1119
1120 if(!(flags & AVSEEK_FLAG_ANY)){
1121 while(m>=0 && m<nb_entries && !(entries[m].flags & AVINDEX_KEYFRAME)){
1122 m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
1123 }
1124 }
1118 1125
1119 if(m == nb_entries) 1126 if(m == nb_entries)
1120 return -1; 1127 return -1;
1121 return m; 1128 return m;
1122 } 1129 }
1150 1157
1151 st= s->streams[stream_index]; 1158 st= s->streams[stream_index];
1152 if(st->index_entries){ 1159 if(st->index_entries){
1153 AVIndexEntry *e; 1160 AVIndexEntry *e;
1154 1161
1155 index= av_index_search_timestamp(st, target_ts, 1); 1162 index= av_index_search_timestamp(st, target_ts, flags | AVSEEK_FLAG_BACKWARD); //FIXME whole func must be checked for non keyframe entries in index case, especially read_timestamp()
1156 index= FFMAX(index, 0); 1163 index= FFMAX(index, 0);
1157 e= &st->index_entries[index]; 1164 e= &st->index_entries[index];
1158 1165
1159 if(e->timestamp <= target_ts || e->pos == e->min_distance){ 1166 if(e->timestamp <= target_ts || e->pos == e->min_distance){
1160 pos_min= e->pos; 1167 pos_min= e->pos;
1164 pos_min,ts_min); 1171 pos_min,ts_min);
1165 #endif 1172 #endif
1166 }else{ 1173 }else{
1167 assert(index==0); 1174 assert(index==0);
1168 } 1175 }
1169 index++; 1176
1170 if(index < st->nb_index_entries){ 1177 index= av_index_search_timestamp(st, target_ts, flags & ~AVSEEK_FLAG_BACKWARD);
1178 assert(index < st->nb_index_entries);
1179 if(index >= 0){
1171 e= &st->index_entries[index]; 1180 e= &st->index_entries[index];
1172 assert(e->timestamp >= target_ts); 1181 assert(e->timestamp >= target_ts);
1173 pos_max= e->pos; 1182 pos_max= e->pos;
1174 ts_max= e->timestamp; 1183 ts_max= e->timestamp;
1175 pos_limit= pos_max - e->min_distance; 1184 pos_limit= pos_max - e->min_distance;
1314 } 1323 }
1315 s->index_built = 1; 1324 s->index_built = 1;
1316 } 1325 }
1317 1326
1318 st = s->streams[stream_index]; 1327 st = s->streams[stream_index];
1319 index = av_index_search_timestamp(st, timestamp, flags & AVSEEK_FLAG_BACKWARD); 1328 index = av_index_search_timestamp(st, timestamp, flags);
1320 if (index < 0) 1329 if (index < 0)
1321 return -1; 1330 return -1;
1322 1331
1323 /* now we have found the index, we can seek */ 1332 /* now we have found the index, we can seek */
1324 ie = &st->index_entries[index]; 1333 ie = &st->index_entries[index];