Fix memory leak in audioc.c.
[paraslash.git] / ogg_afh_common.c
index 353bc9bb51a3e18a5e26fac359dd8aebfdcbbb8e..db82eda3f2d7394e111f4d0cf02d9a5f5fce4a79 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2004-2010 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2004-2012 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -37,13 +37,16 @@ static int process_packets_2_and_3(ogg_sync_state *oss,
                         * apparent at packetout.
                         */
                        ogg_stream_pagein(stream, &page);
+                       PARA_INFO_LOG("ogg page serial: %d\n",
+                               ogg_page_serialno(&page));
                        while (i < 2) {
                                ret = ogg_stream_packetout(stream, &packet);
                                if (ret == 0)
                                        break;
                                if (ret < 0)
                                        return -E_STREAM_PACKETOUT;
-                               ret = ci->packet_callback(&packet, i + 1, afhi,
+                               ret = ci->packet_callback(&packet, i + 1,
+                                       ogg_page_serialno(&page), afhi,
                                        ci->private_data);
                                if (ret < 0)
                                        return ret;
@@ -64,9 +67,8 @@ static int process_ogg_packets(ogg_sync_state *oss, struct afh_info *afhi,
        ogg_page page;
        int ret;
 
-       ret = -E_SYNC_PAGEOUT;
        if (ogg_sync_pageout(oss, &page) != 1)
-               goto out;
+               return -E_SYNC_PAGEOUT;
 
        ret = ogg_page_serialno(&page);
        ogg_stream_init(&stream, ret);
@@ -78,30 +80,28 @@ static int process_ogg_packets(ogg_sync_state *oss, struct afh_info *afhi,
        ret = -E_STREAM_PACKETOUT;
        if (ogg_stream_packetout(&stream, &packet) != 1)
                goto out;
-       ret = ci->packet_callback(&packet, 0, afhi, ci->private_data);
+       ret = ci->packet_callback(&packet, 0, ogg_page_serialno(&page),
+               afhi, ci->private_data);
        if (ret < 0)
                goto out;
        ret = process_packets_2_and_3(oss, &stream, afhi, ci);
        if (ret < 0)
                goto out;
-       afhi->header_offset = 0;
-       afhi->header_len = oss->returned;
        ret = 1;
 out:
        ogg_stream_clear(&stream);
        return ret;
 }
 
-static void set_chunk_tv(int num_frames, int num_chunks, int frequency,
+static void set_chunk_tv(int frames_per_chunk, int frequency,
                struct timeval *result)
 {
-       uint64_t x = (uint64_t)num_frames * 1000 * 1000
-               / frequency / num_chunks;
+       uint64_t x = (uint64_t)frames_per_chunk * 1000 * 1000 / frequency;
 
        result->tv_sec = x / 1000 / 1000;
        result->tv_usec = x % (1000 * 1000);
-       PARA_INFO_LOG("%d chunks, chunk time: %lums\n", num_chunks,
-               tv2ms(result));
+       PARA_INFO_LOG("%d frames per chunk, chunk time: %lums\n",
+               frames_per_chunk, tv2ms(result));
 }
 
 /**
@@ -111,9 +111,9 @@ static void set_chunk_tv(int num_frames, int num_chunks, int frequency,
  * given by \a map and \a numbytes and passes each packet to the callback
  * defined by \a ci.
  *
- * If the packet callback indicates success, the chunk table is built.  Chunk
- * zero contains the first three ogg packets while all other chunks consist of
- * exactly one ogg page.
+ * If the packet callback indicates success and \a afhi is not \p NULL, the
+ * chunk table is built. Chunk zero contains the first three ogg packets while
+ * all other chunks consist of exactly one ogg page.
  *
  * \param map Audio file data.
  * \param numbytes The length of \a map.
@@ -144,9 +144,12 @@ int ogg_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
        ret = process_ogg_packets(&oss, afhi, ci);
        if (ret < 0)
                goto out;
+       if (!afhi)
+               goto out;
+       afhi->header_len = oss.returned;
        oss.returned = 0;
        oss.fill = numbytes;
-       /* count ogg packages and get duration of the file */
+       /* 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);
        PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
@@ -164,10 +167,10 @@ int ogg_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
        afhi->chunk_table[1] = afhi->header_len;
        oss.returned = afhi->header_len;
        oss.fill = numbytes;
-       for (i = 0, j = 1; ogg_sync_pageseek(&oss, &op) > 0; i++) {
+       for (j = 1; ogg_sync_pageseek(&oss, &op) > 0; /* nothing */) {
                int granule = ogg_page_granulepos(&op);
 
-               while (granule > j * frames_per_chunk) {
+               while (granule >= (j + 1) * frames_per_chunk) {
                        j++;
                        if (j >= ct_size) {
                                ct_size *= 2;
@@ -179,7 +182,7 @@ int ogg_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
                }
        }
        afhi->chunks_total = j;
-       set_chunk_tv(num_frames, j, afhi->frequency, &afhi->chunk_tv);
+       set_chunk_tv(frames_per_chunk, afhi->frequency, &afhi->chunk_tv);
        ret = 0;
 out:
        ogg_sync_clear(&oss);