paraslash 0.7.3
[paraslash.git] / ogg_afh_common.c
index 46465de1208b3c8edbb09972550bf4277667c7db..0a27a4acebc1652da03dea1573bf337eaf9c52a9 100644 (file)
@@ -124,8 +124,9 @@ int oac_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
        ogg_sync_state oss;
        ogg_page op;
        char *buf;
-       int ret, i, j, frames_per_chunk, ct_size;
-       long long unsigned num_frames = 0;
+       int ret, i, j, frames_per_chunk, ct_size, prev_pageno = 0;
+       long long unsigned granule_skip = 0, num_frames = 0;
+       int64_t granule = 0, prev_granule = 0;
 
        ogg_sync_init(&oss);
        ret = -E_OGG_SYNC;
@@ -145,8 +146,17 @@ int oac_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
        oss.returned = 0;
        oss.fill = numbytes;
        /* count ogg pages and get duration of the file */
-       for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
-               num_frames = ogg_page_granulepos(&op);
+       for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++) {
+               int this_pageno = ogg_page_pageno(&op);
+               int64_t this_granule = ogg_page_granulepos(&op);
+               if (this_granule >= 0)
+                       granule = this_granule;
+               if (i > 0 && this_pageno != prev_pageno + 1) /* hole */
+                       granule_skip += granule - prev_granule;
+               prev_pageno = this_pageno;
+               prev_granule = granule;
+       }
+       num_frames = granule - granule_skip;
        PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
        ret = -E_OGG_EMPTY;
        if (i == 0)
@@ -154,24 +164,24 @@ int oac_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
        afhi->seconds_total = num_frames / afhi->frequency;
        /* use roughly one page per chunk */
        frames_per_chunk = num_frames / i;
-       PARA_INFO_LOG("%" PRIu32 "seconds, %d frames/chunk\n",
+       PARA_INFO_LOG("%" PRIu32 " seconds, %d frames/chunk\n",
                afhi->seconds_total, frames_per_chunk);
        ct_size = 250;
-       afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
+       afhi->chunk_table = arr_alloc(ct_size, sizeof(uint32_t));
        afhi->chunk_table[0] = 0;
        afhi->chunk_table[1] = afhi->header_len;
        oss.returned = afhi->header_len;
        oss.fill = numbytes;
        for (j = 1; ogg_sync_pageseek(&oss, &op) > 0; /* nothing */) {
-               int granule = ogg_page_granulepos(&op);
+               granule = ogg_page_granulepos(&op);
 
                while (granule >= (j + 1) * frames_per_chunk) {
                        j++;
                        if (j >= ct_size) {
                                ct_size *= 2;
-                               afhi->chunk_table = para_realloc(
+                               afhi->chunk_table = arr_realloc(
                                        afhi->chunk_table,
-                                       ct_size * sizeof(uint32_t));
+                                       ct_size, sizeof(uint32_t));
                        }
                        afhi->chunk_table[j] = oss.returned;
                }
@@ -355,9 +365,9 @@ struct oac_custom_header {
  *
  * \sa \ref oac_custom_header_init().
  */
-struct oac_custom_header *oac_custom_header_new(void)
+__malloc struct oac_custom_header *oac_custom_header_new(void)
 {
-       return para_calloc(sizeof(struct oac_custom_header));
+       return zalloc(sizeof(struct oac_custom_header));
 }
 
 /**