diff options
Diffstat (limited to 'apps/codecs/libasf/asf.c')
-rw-r--r-- | apps/codecs/libasf/asf.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/apps/codecs/libasf/asf.c b/apps/codecs/libasf/asf.c index 18503218b7..65d3fd8deb 100644 --- a/apps/codecs/libasf/asf.c +++ b/apps/codecs/libasf/asf.c | |||
@@ -376,3 +376,55 @@ int asf_get_timestamp(int *duration) | |||
376 | 376 | ||
377 | return send_time; | 377 | return send_time; |
378 | } | 378 | } |
379 | |||
380 | /*entry point for seeks*/ | ||
381 | int seek(int ms, asf_waveformatex_t* wfx) | ||
382 | { | ||
383 | int time, duration, delta, temp, count=0; | ||
384 | |||
385 | /*estimate packet number from bitrate*/ | ||
386 | int initial_packet = ci->curpos/wfx->packet_size; | ||
387 | int packet_num = (((int64_t)ms)*(wfx->bitrate>>3))/wfx->packet_size/1000; | ||
388 | int last_packet = ci->id3->filesize / wfx->packet_size; | ||
389 | |||
390 | if (packet_num > last_packet) { | ||
391 | packet_num = last_packet; | ||
392 | } | ||
393 | |||
394 | /*calculate byte address of the start of that packet*/ | ||
395 | int packet_offset = packet_num*wfx->packet_size; | ||
396 | |||
397 | /*seek to estimated packet*/ | ||
398 | ci->seek_buffer(ci->id3->first_frame_offset+packet_offset); | ||
399 | temp = ms; | ||
400 | while (1) | ||
401 | { | ||
402 | /*for very large files it can be difficult and unimportant to find the exact packet*/ | ||
403 | count++; | ||
404 | |||
405 | /*check the time stamp of our packet*/ | ||
406 | time = asf_get_timestamp(&duration); | ||
407 | DEBUGF("seeked to %d ms with duration %d\n", time, duration); | ||
408 | |||
409 | if (time < 0) { | ||
410 | /*unknown error, try to recover*/ | ||
411 | DEBUGF("UKNOWN SEEK ERROR\n"); | ||
412 | ci->seek_buffer(ci->id3->first_frame_offset+initial_packet*wfx->packet_size); | ||
413 | /*seek failed so return time stamp of the initial packet*/ | ||
414 | return asf_get_timestamp(&duration); | ||
415 | } | ||
416 | |||
417 | if ((time+duration>=ms && time<=ms) || count > 10) { | ||
418 | DEBUGF("Found our packet! Now at %d packet\n", packet_num); | ||
419 | return time; | ||
420 | } else { | ||
421 | /*seek again*/ | ||
422 | delta = ms-time; | ||
423 | /*estimate new packet number from bitrate and our current position*/ | ||
424 | temp += delta; | ||
425 | packet_num = ((temp/1000)*(wfx->bitrate>>3) - (wfx->packet_size>>1))/wfx->packet_size; //round down! | ||
426 | packet_offset = packet_num*wfx->packet_size; | ||
427 | ci->seek_buffer(ci->id3->first_frame_offset+packet_offset); | ||
428 | } | ||
429 | } | ||
430 | } | ||