Create three ogg pages when skipping vorbis comments.
[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 PARA_DEBUG_LOG("next input page (header/body): %lu/%lu\n",
36 page.header_len, page.body_len);
37 /*
38 * We can ignore any errors here as they'll also become
39 * apparent at packetout.
40 */
41 ogg_stream_pagein(stream, &page);
42 PARA_DEBUG_LOG("ogg page serial: %d\n",
43 ogg_page_serialno(&page));
44 while (i < 2) {
45 ret = ogg_stream_packetout(stream, &packet);
46 PARA_DEBUG_LOG("packet #%d: %lu bytes\n", i + 1,
47 packet.bytes);
48 if (ret == 0)
49 break;
50 if (ret < 0)
51 return -E_STREAM_PACKETOUT;
52 ret = ci->packet_callback(&packet, i + 1,
53 ogg_page_serialno(&page), afhi,
54 ci->private_data);
55 if (ret < 0)
56 return ret;
57 if (ret == 0) /* header complete */
58 return 1;
59 i++;
60 }
61 }
62 }
63 return 1;
64 }
65
66 static int process_ogg_packets(ogg_sync_state *oss, struct afh_info *afhi,
67 struct ogg_afh_callback_info *ci)
68 {
69 ogg_packet packet;
70 ogg_stream_state stream;
71 ogg_page page;
72 int ret;
73
74 if (ogg_sync_pageout(oss, &page) != 1)
75 return -E_SYNC_PAGEOUT;
76 PARA_DEBUG_LOG("first input page (header/body): %lu/%lu\n",
77 page.header_len, page.body_len);
78
79 ret = ogg_page_serialno(&page);
80 ogg_stream_init(&stream, ret);
81
82 ret = -E_STREAM_PAGEIN;
83 if (ogg_stream_pagein(&stream, &page) < 0)
84 goto out;
85
86 ret = -E_STREAM_PACKETOUT;
87 if (ogg_stream_packetout(&stream, &packet) != 1)
88 goto out;
89 PARA_DEBUG_LOG("packet #0: %lu bytes\n", packet.bytes);
90 ret = ci->packet_callback(&packet, 0, ogg_page_serialno(&page),
91 afhi, ci->private_data);
92 if (ret < 0)
93 goto out;
94 ret = process_packets_2_and_3(oss, &stream, afhi, ci);
95 if (ret < 0)
96 goto out;
97 ret = 1;
98 out:
99 ogg_stream_clear(&stream);
100 return ret;
101 }
102
103 static void set_chunk_tv(int num_frames, int num_chunks, int frequency,
104 struct timeval *result)
105 {
106 uint64_t x = (uint64_t)num_frames * 1000 * 1000
107 / frequency / num_chunks;
108
109 result->tv_sec = x / 1000 / 1000;
110 result->tv_usec = x % (1000 * 1000);
111 PARA_INFO_LOG("%d chunks, chunk time: %lums\n", num_chunks,
112 tv2ms(result));
113 }
114
115 /**
116 * Pass first three ogg packets to callback and build the chunk table.
117 *
118 * This function extracts the first three ogg packets of the audio data
119 * given by \a map and \a numbytes and passes each packet to the callback
120 * defined by \a ci.
121 *
122 * If the packet callback indicates success and \a afhi is not \p NULL, the
123 * chunk table is built. Chunk zero contains the first three ogg packets while
124 * all other chunks consist of exactly one ogg page.
125 *
126 * \param map Audio file data.
127 * \param numbytes The length of \a map.
128 * \param afhi Passed to the packet callback, contains chunk table.
129 * \param ci The callback structure.
130 *
131 * \return Standard.
132 */
133 int ogg_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
134 struct ogg_afh_callback_info *ci)
135 {
136 ogg_sync_state oss;
137 ogg_page op;
138 long len = numbytes;
139 char *buf;
140 int ret, i, j, frames_per_chunk, ct_size;
141 long long unsigned num_frames = 0;
142
143 ogg_sync_init(&oss);
144 ret = -E_OGG_SYNC;
145 buf = ogg_sync_buffer(&oss, len);
146 if (!buf)
147 goto out;
148 memcpy(buf, map, len);
149 ret = -E_OGG_SYNC;
150 if (ogg_sync_wrote(&oss, len) < 0)
151 goto out;
152 ret = process_ogg_packets(&oss, afhi, ci);
153 if (ret < 0)
154 goto out;
155 if (!afhi)
156 goto out;
157 afhi->header_len = oss.returned;
158 oss.returned = 0;
159 oss.fill = numbytes;
160 /* count ogg packages and get duration of the file */
161 for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
162 num_frames = ogg_page_granulepos(&op);
163 PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
164 ret = -E_OGG_EMPTY;
165 if (i == 0)
166 goto out;
167 afhi->seconds_total = num_frames / afhi->frequency;
168 /* use roughly one page per chunk */
169 frames_per_chunk = num_frames / i;
170 PARA_INFO_LOG("%lu seconds, %d frames/chunk\n",
171 afhi->seconds_total, frames_per_chunk);
172 ct_size = 250;
173 afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
174 afhi->chunk_table[0] = 0;
175 afhi->chunk_table[1] = afhi->header_len;
176 oss.returned = afhi->header_len;
177 oss.fill = numbytes;
178 for (j = 1; ogg_sync_pageseek(&oss, &op) > 0; /* nothing */) {
179 int granule = ogg_page_granulepos(&op);
180
181 while (granule > j * frames_per_chunk) {
182 j++;
183 if (j >= ct_size) {
184 ct_size *= 2;
185 afhi->chunk_table = para_realloc(
186 afhi->chunk_table,
187 ct_size * sizeof(uint32_t));
188 }
189 afhi->chunk_table[j] = oss.returned;
190 }
191 }
192 afhi->chunks_total = j;
193 set_chunk_tv(num_frames, j, afhi->frequency, &afhi->chunk_tv);
194 ret = 0;
195 out:
196 ogg_sync_clear(&oss);
197 return ret;
198 }