1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/* This program is licensed under the GNU Library General Public License, version 2
*
* (c) 2000-2001 Michael Smith <[email protected]>
*
* Modified by Warren Spits <[email protected]>
* - Handles vorbis files that are truncated or missing an eos flag.
*
* Comment editing backend, suitable for use by nice frontend interfaces.
*
* last modified: $Id$
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ogg/ogg.h>
#include <vorbis/codec.h>
#include "vcedit.h"
#define CHUNKSIZE 4096
vcedit_state *vcedit_new_state(void)
{
vcedit_state *state = malloc(sizeof(vcedit_state));
memset(state, 0, sizeof(vcedit_state));
return state;
}
char *vcedit_error(vcedit_state *state)
{
return state->lasterror;
}
vorbis_comment *vcedit_comments(vcedit_state *state)
{
return state->vc;
}
static void vcedit_clear_internals(vcedit_state *state)
{
if(state->vc)
{
vorbis_comment_clear(state->vc);
free(state->vc);
state->vc=NULL;
}
if(state->os)
{
ogg_stream_clear(state->os);
free(state->os);
state->os=NULL;
}
if(state->oy)
{
ogg_sync_clear(state->oy);
free(state->oy);
state->oy=NULL;
}
}
void vcedit_clear(vcedit_state *state)
{
if(state)
{
vcedit_clear_internals(state);
free(state);
}
}
int vcedit_open(vcedit_state *state, FILE *in)
{
return vcedit_open_callbacks(state, (void *)in,
(vcedit_read_func)fread, (vcedit_write_func)fwrite);
}
int vcedit_open_callbacks(vcedit_state *state, void *in,
vcedit_read_func read_func, vcedit_write_func write_func)
{
char *buffer;
int bytes,i;
ogg_packet *header;
ogg_packet header_main;
ogg_packet header_comments;
ogg_packet header_codebooks;
ogg_page og;
vorbis_info vi;
state->in = in;
state->read = read_func;
state->write = write_func;
state->lasterror = 0;
state->oy = malloc(sizeof(ogg_sync_state));
ogg_sync_init(state->oy);
buffer = ogg_sync_buffer(state->oy, CHUNKSIZE);
bytes = state->read(buffer, 1, CHUNKSIZE, state->in);
ogg_sync_wrote(state->oy, bytes);
if(ogg_sync_pageout(state->oy, &og) != 1)
{
if(bytes<CHUNKSIZE)
state->lasterror = "Input truncated or empty.";
else
state->lasterror = "Input is not an Ogg bitstream.";
goto err;
}
state->serial = ogg_page_serialno(&og);
state->os = malloc(sizeof(ogg_stream_state));
ogg_stream_init(state->os, state->serial);
vorbis_info_init(&vi);
state->vc = malloc(sizeof(vorbis_comment));
vorbis_comment_init(state->vc);
if(ogg_stream_pagein(state->os, &og) < 0)
{
state->lasterror = "Error reading first page of Ogg bitstream.";
goto err;
}
if(ogg_stream_packetout(state->os, &header_main) != 1)
{
state->lasterror = "Error reading initial header packet.";
goto err;
}
if(vorbis_synthesis_headerin(&vi, state->vc, &header_main) < 0)
{
state->lasterror = "Ogg bitstream does not contain vorbis data.";
goto err;
}
state->mainlen = header_main.bytes;
state->mainbuf = malloc(state->mainlen);
memcpy(state->mainbuf, header_main.packet, header_main.bytes);
i = 0;
header = &header_comments;
while(i<2) {
while(i<2) {
int result = ogg_sync_pageout(state->oy, &og);
if(result == 0) break; /* Too little data so far */
else if(result == 1)
{
ogg_stream_pagein(state->os, &og);
while(i<2)
{
result = ogg_stream_packetout(state->os, header);
if(result == 0) break;
if(result == -1)
{
state->lasterror = "Corrupt secondary header.";
goto err;
}
vorbis_synthesis_headerin(&vi, state->vc, header);
if(i==1)
{
state->booklen = header->bytes;
state->bookbuf = malloc(state->booklen);
memcpy(state->bookbuf, header->packet,
header->bytes);
}
i++;
header = &header_codebooks;
}
}
}
buffer = ogg_sync_buffer(state->oy, CHUNKSIZE);
bytes = state->read(buffer, 1, CHUNKSIZE, state->in);
if(bytes == 0 && i < 2)
{
state->lasterror = "EOF before end of vorbis headers.";
goto err;
}
ogg_sync_wrote(state->oy, bytes);
}
/* Headers are done! */
vorbis_info_clear(&vi);
return 0;
err:
vcedit_clear_internals(state);
return -1;
}
int vcedit_write(vcedit_state *state, void *out)
{
ogg_stream_state streamout;
ogg_packet header_main;
ogg_packet header_comments;
ogg_packet header_codebooks;
ogg_page ogout, ogin;
ogg_packet op;
int result, outresult;
char *buffer;
int bytes, eosin=0, eosout=0;
state->lasterror = 0;
header_main.bytes = state->mainlen;
header_main.packet = state->mainbuf;
header_main.b_o_s = 1;
header_main.e_o_s = 0;
header_main.granulepos = 0;
header_codebooks.bytes = state->booklen;
header_codebooks.packet = state->bookbuf;
header_codebooks.b_o_s = 0;
header_codebooks.e_o_s = 0;
header_codebooks.granulepos = 0;
ogg_stream_init(&streamout, state->serial);
vorbis_commentheader_out(state->vc, &header_comments);
ogg_stream_packetin(&streamout, &header_main);
ogg_stream_packetin(&streamout, &header_comments);
ogg_stream_packetin(&streamout, &header_codebooks);
while((result = ogg_stream_flush(&streamout, &ogout)))
{
if(state->write(ogout.header,1,ogout.header_len, out) !=
(size_t) ogout.header_len)
goto cleanup;
if(state->write(ogout.body,1,ogout.body_len, out) !=
(size_t) ogout.body_len)
goto cleanup;
}
/* We copy the first logical stream
* through, rewriting the stream. */
while (1)
{
outresult = eosin ? ogg_stream_flush(&streamout, &ogout) :
ogg_stream_pageout(&streamout, &ogout);
if (outresult > 0)
{
if (state->write(ogout.header,1,ogout.header_len,
out) != (size_t) ogout.header_len)
goto cleanup;
if (state->write(ogout.body,1,ogout.body_len,
out) != (size_t) ogout.body_len)
goto cleanup;
if (ogg_page_eos(&ogout)) eosout = 1;
}
if (outresult != 0) continue;
if (eosout || (eosin && (result == 0))) break;
while (1)
{
result = ogg_stream_packetout(state->os, &op);
if (result < 0) continue;
if (result > 0) ogg_stream_packetin(&streamout, &op);
if (eosin || (result > 0)) break;
while (1)
{
result = ogg_sync_pageout(state->oy, &ogin);
if (result < 0) continue;
if (result > 0)
{
ogg_stream_pagein(state->os, &ogin);
if (ogg_page_eos(&ogin)) eosin = 1;
}
if (eosin || (result > 0)) break;
buffer = ogg_sync_buffer(state->oy, CHUNKSIZE);
bytes = state->read(buffer,1, CHUNKSIZE, state->in);
ogg_sync_wrote(state->oy, bytes);
if (bytes < CHUNKSIZE) eosin = 1;
}
}
}
eosin=0; /* clear it, because not all paths to here do */
eosout=1; /* handle input files that are truncated or without an eos flag */
/* We copy the rest of the stream (other logical streams)
* through, a page at a time. */
while (1)
{
result = ogg_sync_pageout(state->oy, &ogout);
if (result > 0)
{
if (state->write(ogout.header,1,ogout.header_len,
out) != (size_t) ogout.header_len)
goto cleanup;
if (state->write(ogout.body,1,ogout.body_len, out) !=
(size_t) ogout.body_len)
goto cleanup;
}
if (result != 0) continue;
if (eosin) break;
buffer = ogg_sync_buffer(state->oy, CHUNKSIZE);
bytes = state->read(buffer,1, CHUNKSIZE, state->in);
ogg_sync_wrote(state->oy, bytes);
eosin = (bytes < CHUNKSIZE);
}
cleanup:
ogg_stream_clear(&streamout);
ogg_packet_clear(&header_comments);
free(state->mainbuf);
free(state->bookbuf);
vcedit_clear_internals(state);
if(!(eosin && eosout))
{
state->lasterror =
"Error writing stream to output. "
"Output stream may be corrupted or truncated.";
return -1;
}
return 0;
}
|