2 * Copyright (C) 2000-2001 Michael Smith (msmith at xiph org)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation, version 2.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 #include <vorbis/codec.h>
32 #define CHUNKSIZE 4096
34 struct vcedit_state_St {
46 unsigned char *mainbuf;
47 unsigned char *bookbuf;
59 vcedit_state_free (vcedit_state *state)
61 free (state->mainbuf);
62 free (state->bookbuf);
74 vcedit_state_init (vcedit_state *state, const char *filename)
78 strcpy (state->filename, filename);
84 vcedit_state_new (const char *filename)
89 len = strlen (filename);
93 state = malloc (sizeof (vcedit_state) + len + 1);
97 memset (state, 0, sizeof (vcedit_state));
99 if (!vcedit_state_init (state, filename)) {
100 vcedit_state_free (state);
108 vcedit_comments (vcedit_state *state)
110 return state->opened ? &state->vc : NULL;
114 vcedit_clear_internals (vcedit_state *state)
116 ogg_stream_clear (&state->os);
117 ogg_sync_clear (&state->oy);
119 vorbis_info_clear (&state->vi);
120 vorbis_comment_clear (&state->vc);
122 free (state->vendor);
123 state->vendor = NULL;
125 free (state->mainbuf);
126 state->mainbuf = NULL;
129 free (state->bookbuf);
130 state->bookbuf = NULL;
134 state->opened = false;
138 vcedit_state_ref (vcedit_state *state)
144 vcedit_state_unref (vcedit_state *state)
146 if (--state->refcount)
150 vcedit_clear_internals (state);
152 vcedit_state_free (state);
155 /* Next two functions pulled straight from libvorbis, apart from one change
156 * - we don't want to overwrite the vendor string.
159 _v_writestring (oggpack_buffer *o, char *s, int len)
162 oggpack_write (o, *s++, 8);
166 _commentheader_out (vorbis_comment *vc, char *vendor, ogg_packet *op)
172 oggpack_writeinit (&opb);
175 oggpack_write (&opb, 0x03, 8);
176 _v_writestring (&opb, "vorbis", 6);
179 oggpack_write (&opb, strlen (vendor), 32);
180 _v_writestring (&opb, vendor, strlen (vendor));
183 oggpack_write (&opb, vc->comments, 32);
185 for (i = 0; i < vc->comments; i++) {
186 if (!vc->user_comments[i])
187 oggpack_write (&opb, 0, 32);
189 oggpack_write (&opb, vc->comment_lengths[i], 32);
190 _v_writestring (&opb, vc->user_comments[i],
191 vc->comment_lengths[i]);
195 oggpack_write (&opb, 1, 1);
197 op->packet = _ogg_malloc (oggpack_bytes (&opb));
198 memcpy (op->packet, opb.buffer, oggpack_bytes (&opb));
200 op->bytes = oggpack_bytes (&opb);
205 oggpack_writeclear (&opb);
211 _blocksize (vcedit_state *s, ogg_packet *p)
215 this = vorbis_packet_blocksize (&s->vi, p);
218 ret = (this + s->prevW) / 4;
226 _fetch_next_packet (vcedit_state *s, ogg_packet *p, ogg_page *page)
231 result = ogg_stream_packetout (&s->os, p);
238 while (ogg_sync_pageout (&s->oy, page) != 1) {
239 buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
240 bytes = fread (buffer, 1, CHUNKSIZE, s->in);
241 ogg_sync_wrote (&s->oy, bytes);
243 if (!bytes && feof (s->in))
247 if (ogg_page_eos (page))
249 else if (ogg_page_serialno (page) != s->serial) {
255 ogg_stream_pagein (&s->os, page);
257 return _fetch_next_packet (s, p, page);
261 vcedit_open (vcedit_state *state)
265 ogg_packet header_main, header_comments, header_codebooks;
268 size_t bytes, total = 0;
271 state->in = fopen (state->filename, "rb");
273 return VCEDIT_ERR_OPEN;
275 ogg_sync_init (&state->oy);
278 /* Bail if we don't find data in the first 40 kB */
279 if (feof (state->in) || total >= (CHUNKSIZE * 10)) {
280 ogg_sync_clear (&state->oy);
282 return VCEDIT_ERR_INVAL;
285 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
287 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
290 ogg_sync_wrote (&state->oy, bytes);
291 } while (ogg_sync_pageout (&state->oy, &og) != 1);
293 state->serial = ogg_page_serialno (&og);
295 ogg_stream_init (&state->os, state->serial);
296 vorbis_info_init (&state->vi);
297 vorbis_comment_init (&state->vc);
299 if (ogg_stream_pagein (&state->os, &og) < 0) {
300 ret = VCEDIT_ERR_INVAL;
304 if (ogg_stream_packetout (&state->os, &header_main) != 1) {
305 ret = VCEDIT_ERR_INVAL;
309 if (vorbis_synthesis_headerin (&state->vi, &state->vc, &header_main) < 0) {
310 ret = VCEDIT_ERR_INVAL;
314 state->mainlen = header_main.bytes;
315 state->mainbuf = malloc (state->mainlen);
316 memcpy (state->mainbuf, header_main.packet, header_main.bytes);
318 header = &header_comments;
324 result = ogg_sync_pageout (&state->oy, &og);
326 break; /* Too little data so far */
331 ogg_stream_pagein (&state->os, &og);
334 result = ogg_stream_packetout (&state->os, header);
339 ret = VCEDIT_ERR_INVAL;
343 vorbis_synthesis_headerin (&state->vi, &state->vc, header);
346 state->booklen = header->bytes;
347 state->bookbuf = malloc (state->booklen);
348 memcpy (state->bookbuf, header->packet, header->bytes);
351 header = &header_codebooks;
355 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
356 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
358 if (!bytes && feof (state->in) && i < 2) {
359 ret = VCEDIT_ERR_INVAL;
363 ogg_sync_wrote (&state->oy, bytes);
366 /* Copy the vendor tag */
367 state->vendor = strdup (state->vc.vendor);
369 /* Headers are done! */
370 state->opened = true;
372 return VCEDIT_ERR_SUCCESS;
375 vcedit_clear_internals (state);
381 vcedit_write (vcedit_state *state)
383 ogg_stream_state streamout;
384 ogg_packet header_main, header_comments, header_codebooks, op;
385 ogg_page ogout, ogin;
386 ogg_int64_t granpos = 0;
388 char *buffer, tmpfile[PATH_MAX];
389 int s, result, bytes, needflush = 0, needout = 0;
393 return VCEDIT_ERR_INVAL;
395 strcpy (tmpfile, state->filename);
396 strcat (tmpfile, ".XXXXXX");
398 s = mkstemp (tmpfile);
400 return VCEDIT_ERR_TMPFILE;
402 out = fdopen (s, "wb");
407 return VCEDIT_ERR_TMPFILE;
410 state->prevW = state->extrapage = state->eosin = 0;
412 header_main.bytes = state->mainlen;
413 header_main.packet = state->mainbuf;
414 header_main.b_o_s = 1;
415 header_main.e_o_s = 0;
416 header_main.granulepos = 0;
418 header_codebooks.bytes = state->booklen;
419 header_codebooks.packet = state->bookbuf;
420 header_codebooks.b_o_s = 0;
421 header_codebooks.e_o_s = 0;
422 header_codebooks.granulepos = 0;
424 ogg_stream_init (&streamout, state->serial);
426 _commentheader_out (&state->vc, state->vendor, &header_comments);
428 ogg_stream_packetin (&streamout, &header_main);
429 ogg_stream_packetin (&streamout, &header_comments);
430 ogg_stream_packetin (&streamout, &header_codebooks);
432 while ((result = ogg_stream_flush (&streamout, &ogout))) {
433 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
434 if (tmp != (size_t) ogout.header_len)
437 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
438 if (tmp != (size_t) ogout.body_len)
442 while (_fetch_next_packet (state, &op, &ogin)) {
445 size = _blocksize (state, &op);
449 if (ogg_stream_flush (&streamout, &ogout)) {
450 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
451 if (tmp != (size_t) ogout.header_len)
454 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
455 if (tmp != (size_t) ogout.body_len)
458 } else if (needout) {
459 if (ogg_stream_pageout (&streamout, &ogout)) {
460 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
461 if (tmp != (size_t) ogout.header_len)
464 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
465 if (tmp != (size_t) ogout.body_len)
470 needflush = needout = 0;
472 if (op.granulepos == -1) {
473 op.granulepos = granpos;
474 ogg_stream_packetin (&streamout, &op);
476 /* granulepos is set, validly. Use it, and force a flush to
477 * account for shortened blocks (vcut) when appropriate
479 if (granpos > op.granulepos) {
480 granpos = op.granulepos;
481 ogg_stream_packetin (&streamout, &op);
484 ogg_stream_packetin (&streamout, &op);
492 while (ogg_stream_flush (&streamout, &ogout)) {
493 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
494 if (tmp != (size_t) ogout.header_len)
497 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
498 if (tmp != (size_t) ogout.body_len)
502 if (state->extrapage) {
503 tmp = fwrite (ogin.header, 1, ogin.header_len, out);
504 if (tmp != (size_t) ogin.header_len)
507 tmp = fwrite (ogin.body, 1, ogin.body_len, out);
508 if (tmp != (size_t) ogin.body_len)
512 /* clear it, because not all paths to here do */
515 while (!state->eosin) { /* We reached eos, not eof */
516 /* We copy the rest of the stream (other logical streams)
517 * through, a page at a time.
519 while ((result = ogg_sync_pageout (&state->oy, &ogout))) {
523 /* Don't bother going through the rest, we can just
524 * write the page out now
526 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
527 if (tmp != (size_t) ogout.header_len)
530 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
531 if (tmp != (size_t) ogout.body_len)
535 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
536 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
537 ogg_sync_wrote (&state->oy, bytes);
539 state->eosin = !bytes && feof (state->in);
545 unlink (state->filename);
546 rename (tmpfile, state->filename);
549 ogg_stream_clear (&streamout);
551 /* We don't ogg_packet_clear() this, because the memory was
552 * allocated in _commentheader_out(), so we mirror that here
554 _ogg_free (header_comments.packet);
556 free (state->mainbuf);
557 free (state->bookbuf);
559 state->mainbuf = state->bookbuf = NULL;
562 return VCEDIT_ERR_INVAL;
564 vcedit_clear_internals (state);
566 return (vcedit_open (state) == VCEDIT_ERR_SUCCESS) ?
567 VCEDIT_ERR_SUCCESS : VCEDIT_ERR_REOPEN;