Speed up the oggdec filter and avoid wasting tons of memory.
[paraslash.git] / ogg_afh_common.c
1 /*
2  * Copyright (C) 2004-2011 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file ogg_afh_common.c Functions common to ogg/vorbis and ogg/speex. */
8
9 #include <ogg/ogg.h>
10 #include <regex.h>
11
12 #include "para.h"
13 #include "afh.h"
14 #include "error.h"
15 #include "string.h"
16 #include "ogg_afh_common.h"
17
18
19 /* Taken from decoder_example.c of libvorbis-1.2.3. */
20 static int process_packets_2_and_3(ogg_sync_state *oss,
21                 ogg_stream_state *stream, struct afh_info *afhi,
22                 struct ogg_afh_callback_info *ci)
23 {
24         ogg_page page;
25         ogg_packet packet;
26         int i = 0;
27
28         while (i < 2) {
29                 while (i < 2) {
30                         int ret = ogg_sync_pageout(oss, &page);
31                         if (ret == 0)
32                                 break; /* Need more data */
33                         if (ret != 1)
34                                 continue;
35                         /*
36                          * We can ignore any errors here as they'll also become
37                          * apparent at packetout.
38                          */
39                         ogg_stream_pagein(stream, &page);
40                         while (i < 2) {
41                                 ret = ogg_stream_packetout(stream, &packet);
42                                 if (ret == 0)
43                                         break;
44                                 if (ret < 0)
45                                         return -E_STREAM_PACKETOUT;
46                                 ret = ci->packet_callback(&packet, i + 1, afhi,
47                                         ci->private_data);
48                                 if (ret < 0)
49                                         return ret;
50                                 if (ret == 0) /* header complete */
51                                         return 1;
52                                 i++;
53                         }
54                 }
55         }
56         return 1;
57 }
58
59 static int process_ogg_packets(ogg_sync_state *oss, struct afh_info *afhi,
60                 struct ogg_afh_callback_info *ci)
61 {
62         ogg_packet packet;
63         ogg_stream_state stream;
64         ogg_page page;
65         int ret;
66
67         if (ogg_sync_pageout(oss, &page) != 1)
68                 return -E_SYNC_PAGEOUT;
69
70         ret = ogg_page_serialno(&page);
71         ogg_stream_init(&stream, ret);
72
73         ret = -E_STREAM_PAGEIN;
74         if (ogg_stream_pagein(&stream, &page) < 0)
75                 goto out;
76
77         ret = -E_STREAM_PACKETOUT;
78         if (ogg_stream_packetout(&stream, &packet) != 1)
79                 goto out;
80         ret = ci->packet_callback(&packet, 0, afhi, ci->private_data);
81         if (ret < 0)
82                 goto out;
83         ret = process_packets_2_and_3(oss, &stream, afhi, ci);
84         if (ret < 0)
85                 goto out;
86         afhi->header_offset = 0;
87         afhi->header_len = oss->returned;
88         ret = 1;
89 out:
90         ogg_stream_clear(&stream);
91         return ret;
92 }
93
94 static void set_chunk_tv(int frames_per_chunk, int frequency,
95                 struct timeval *result)
96 {
97         uint64_t x = (uint64_t)frames_per_chunk * 1000 * 1000 / frequency;
98
99         result->tv_sec = x / 1000 / 1000;
100         result->tv_usec = x % (1000 * 1000);
101         PARA_INFO_LOG("%d frames per chunk, chunk time: %lums\n",
102                 frames_per_chunk, tv2ms(result));
103 }
104
105 /**
106  * Pass first three ogg packets to callback and build the chunk table.
107  *
108  * This function extracts the first three ogg packets of the audio data
109  * given by \a map and \a numbytes and passes each packet to the callback
110  * defined by \a ci.
111  *
112  * If the packet callback indicates success, the chunk table is built.  Chunk
113  * zero contains the first three ogg packets while all other chunks consist of
114  * exactly one ogg page.
115  *
116  * \param map Audio file data.
117  * \param numbytes The length of \a map.
118  * \param afhi Passed to the packet callback, contains chunk table.
119  * \param ci The callback structure.
120  *
121  * \return Standard.
122  */
123 int ogg_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
124                 struct ogg_afh_callback_info *ci)
125 {
126         ogg_sync_state oss;
127         ogg_page op;
128         long len = numbytes;
129         char *buf;
130         int ret, i, j, frames_per_chunk, ct_size;
131         long long unsigned num_frames = 0;
132
133         ogg_sync_init(&oss);
134         ret = -E_OGG_SYNC;
135         buf = ogg_sync_buffer(&oss, len);
136         if (!buf)
137                 goto out;
138         memcpy(buf, map, len);
139         ret = -E_OGG_SYNC;
140         if (ogg_sync_wrote(&oss, len) < 0)
141                 goto out;
142         ret = process_ogg_packets(&oss, afhi, ci);
143         if (ret < 0)
144                 goto out;
145         oss.returned = 0;
146         oss.fill = numbytes;
147         /* count ogg pages and get duration of the file */
148         for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
149                 num_frames = ogg_page_granulepos(&op);
150         PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
151         ret = -E_OGG_EMPTY;
152         if (i == 0)
153                 goto out;
154         afhi->seconds_total = num_frames / afhi->frequency;
155         /* use roughly one page per chunk */
156         frames_per_chunk = num_frames / i;
157         PARA_INFO_LOG("%lu seconds, %d frames/chunk\n",
158                 afhi->seconds_total, frames_per_chunk);
159         ct_size = 250;
160         afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
161         afhi->chunk_table[0] = 0;
162         afhi->chunk_table[1] = afhi->header_len;
163         oss.returned = afhi->header_len;
164         oss.fill = numbytes;
165         for (j = 1; ogg_sync_pageseek(&oss, &op) > 0; /* nothing */) {
166                 int granule = ogg_page_granulepos(&op);
167
168                 while (granule > j * frames_per_chunk) {
169                         j++;
170                         if (j >= ct_size) {
171                                 ct_size *= 2;
172                                 afhi->chunk_table = para_realloc(
173                                         afhi->chunk_table,
174                                         ct_size * sizeof(uint32_t));
175                         }
176                         afhi->chunk_table[j] = oss.returned;
177                 }
178         }
179         afhi->chunks_total = j;
180         set_chunk_tv(frames_per_chunk, afhi->frequency, &afhi->chunk_tv);
181         ret = 0;
182 out:
183         ogg_sync_clear(&oss);
184         return ret;
185 }