comparison libmpdemux/aviheader.c @ 14297:85ec4f714596

Use libc qsort to sort ODML index. Based on patch by phillip at phdcam dot com
author reimar
date Sun, 02 Jan 2005 14:29:43 +0000
parents 25142a687b00
children 35001ce5b853
comparison
equal deleted inserted replaced
14296:2c1492daa930 14297:85ec4f714596
42 return 1; 42 return 1;
43 } 43 }
44 return 0; 44 return 0;
45 } 45 }
46 46
47 /** 47 int avi_idx_cmp(const void *elem1,const void *elem2) {
48 * Simple quicksort for AVIINDEXENTRYs 48 register off_t a = AVI_IDX_OFFSET((AVIINDEXENTRY *)elem1);
49 * To avoid too deep recursion, the bigger part is handled iteratively, 49 register off_t b = AVI_IDX_OFFSET((AVIINDEXENTRY *)elem2);
50 * thus limiting recursion to log2(n) levels. 50 return (a > b) - (b > a);
51 * The pivot element is randomized to "even out" otherwise extreme cases.
52 */
53 static void avi_idx_quicksort(AVIINDEXENTRY *idx, int from, int to)
54 {
55 AVIINDEXENTRY temp;
56 int lo;
57 int hi;
58 off_t pivot_ofs;
59 int pivot_idx;
60 while (from < to) {
61 pivot_idx = from;
62 pivot_idx += rand() % (to - from + 1);
63 pivot_ofs = AVI_IDX_OFFSET(&idx[pivot_idx]);
64 lo = to;
65 hi = from;
66 do {
67 while(pivot_ofs < AVI_IDX_OFFSET(&idx[lo])) lo--;
68 while(pivot_ofs > AVI_IDX_OFFSET(&idx[hi])) hi++;
69 if(hi <= lo) {
70 if (hi != lo) {
71 memcpy(&temp, &idx[lo], sizeof(temp));
72 memcpy(&idx[lo], &idx[hi], sizeof(temp));
73 memcpy(&idx[hi], &temp, sizeof(temp));
74 }
75 lo--; hi++;
76 }
77 } while (lo >= hi);
78 if ((lo - from) < (to - hi)) {
79 avi_idx_quicksort(idx, from, lo);
80 from = hi;
81 } else {
82 avi_idx_quicksort(idx, hi, to);
83 to = lo;
84 }
85 }
86 } 51 }
87 52
88 void read_avi_header(demuxer_t *demuxer,int index_mode){ 53 void read_avi_header(demuxer_t *demuxer,int index_mode){
89 sh_audio_t *sh_audio=NULL; 54 sh_audio_t *sh_audio=NULL;
90 sh_video_t *sh_video=NULL; 55 sh_video_t *sh_video=NULL;
533 idx->dwFlags |= (sie->dwSize&0x80000000)?0x0:AVIIF_KEYFRAME; // bit 31 denotes !keyframe 498 idx->dwFlags |= (sie->dwSize&0x80000000)?0x0:AVIIF_KEYFRAME; // bit 31 denotes !keyframe
534 idx++; 499 idx++;
535 } 500 }
536 } 501 }
537 } 502 }
538 avi_idx_quicksort(priv->idx, 0, priv->idx_size-1); 503 qsort(priv->idx, priv->idx_size, sizeof(AVIINDEXENTRY), avi_idx_cmp);
539 504
540 /* 505 /*
541 Hack to work around a "wrong" index in some divx odml files 506 Hack to work around a "wrong" index in some divx odml files
542 (processor_burning.avi as an example) 507 (processor_burning.avi as an example)
543 They have ##dc on non keyframes but the ix00 tells us they are ##db. 508 They have ##dc on non keyframes but the ix00 tells us they are ##db.