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 {
43 char filename[PATH_MAX];
48 unsigned char *mainbuf;
49 unsigned char *bookbuf;
59 vcedit_state_free (vcedit_state *state)
65 free (state->mainbuf);
66 free (state->bookbuf);
78 vcedit_state_init (vcedit_state *state)
82 state->oy = malloc (sizeof (ogg_sync_state));
86 state->os = malloc (sizeof (ogg_stream_state));
90 state->vc = malloc (sizeof (vorbis_comment));
94 state->vi = malloc (sizeof (vorbis_info));
102 vcedit_state_new (const char *filename)
106 state = malloc (sizeof (vcedit_state));
110 memset (state, 0, sizeof (vcedit_state));
112 if (!vcedit_state_init (state)) {
113 vcedit_state_free (state);
117 snprintf (state->filename, sizeof (state->filename),
124 vcedit_comments (vcedit_state *state)
126 return state->opened ? state->vc : NULL;
130 vcedit_clear_internals (vcedit_state *state)
132 ogg_stream_clear (state->os);
133 ogg_sync_clear (state->oy);
135 vorbis_info_clear (state->vi);
136 vorbis_comment_clear (state->vc);
138 free (state->vendor);
139 state->vendor = NULL;
141 free (state->mainbuf);
142 state->mainbuf = NULL;
145 free (state->bookbuf);
146 state->bookbuf = NULL;
150 state->opened = false;
154 vcedit_state_ref (vcedit_state *state)
160 vcedit_state_unref (vcedit_state *state)
162 if (--state->refcount)
165 vcedit_clear_internals (state);
166 vcedit_state_free (state);
169 /* Next two functions pulled straight from libvorbis, apart from one change
170 * - we don't want to overwrite the vendor string.
173 _v_writestring (oggpack_buffer *o, char *s, int len)
176 oggpack_write (o, *s++, 8);
181 _commentheader_out (vorbis_comment *vc, char *vendor, ogg_packet *op)
187 oggpack_writeinit (&opb);
190 oggpack_write (&opb, 0x03, 8);
191 _v_writestring (&opb, "vorbis", 6);
194 oggpack_write (&opb, strlen (vendor), 32);
195 _v_writestring (&opb, vendor, strlen (vendor));
198 oggpack_write (&opb, vc->comments, 32);
200 for (i = 0; i < vc->comments; i++) {
201 if (!vc->user_comments[i])
202 oggpack_write (&opb, 0, 32);
204 oggpack_write (&opb, vc->comment_lengths[i], 32);
205 _v_writestring (&opb, vc->user_comments[i],
206 vc->comment_lengths[i]);
210 oggpack_write (&opb, 1, 1);
212 op->packet = _ogg_malloc (oggpack_bytes (&opb));
213 memcpy (op->packet, opb.buffer, oggpack_bytes (&opb));
215 op->bytes = oggpack_bytes (&opb);
220 oggpack_writeclear (&opb);
226 _blocksize (vcedit_state *s, ogg_packet *p)
230 this = vorbis_packet_blocksize (s->vi, p);
233 ret = (this + s->prevW) / 4;
241 _fetch_next_packet (vcedit_state *s, ogg_packet *p, ogg_page *page)
246 result = ogg_stream_packetout (s->os, p);
254 while (ogg_sync_pageout (s->oy, page) <= 0) {
255 buffer = ogg_sync_buffer (s->oy, CHUNKSIZE);
256 bytes = fread (buffer, 1, CHUNKSIZE, s->in);
257 ogg_sync_wrote (s->oy, bytes);
263 if (ogg_page_eos (page))
265 else if (ogg_page_serialno (page) != s->serial) {
271 ogg_stream_pagein (s->os, page);
273 return _fetch_next_packet (s, p, page);
277 vcedit_open (vcedit_state *state)
284 ogg_packet header_main, header_comments, header_codebooks;
287 state->in = fopen (state->filename, "rb");
289 return VCEDIT_ERR_OPEN;
291 ogg_sync_init (state->oy);
294 buffer = ogg_sync_buffer (state->oy, CHUNKSIZE);
295 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
297 ogg_sync_wrote (state->oy, bytes);
299 if (ogg_sync_pageout (state->oy, &og) == 1)
302 /* Bail if we don't find data in the first 40 kB */
303 if (chunks++ >= 10) {
304 ret = VCEDIT_ERR_INVAL;
310 state->serial = ogg_page_serialno (&og);
312 ogg_stream_init (state->os, state->serial);
313 vorbis_info_init (state->vi);
314 vorbis_comment_init (state->vc);
316 if (ogg_stream_pagein (state->os, &og) < 0) {
317 ret = VCEDIT_ERR_INVAL;
321 if (ogg_stream_packetout (state->os, &header_main) != 1) {
322 ret = VCEDIT_ERR_INVAL;
326 if (vorbis_synthesis_headerin (state->vi, state->vc, &header_main) < 0) {
327 ret = VCEDIT_ERR_INVAL;
331 state->mainlen = header_main.bytes;
332 state->mainbuf = malloc (state->mainlen);
333 memcpy (state->mainbuf, header_main.packet, header_main.bytes);
336 header = &header_comments;
340 int result = ogg_sync_pageout (state->oy, &og);
343 break; /* Too little data so far */
346 ogg_stream_pagein (state->os, &og);
349 result = ogg_stream_packetout (state->os, header);
355 ret = VCEDIT_ERR_INVAL;
359 vorbis_synthesis_headerin (state->vi, state->vc, header);
362 state->booklen = header->bytes;
363 state->bookbuf = malloc (state->booklen);
364 memcpy (state->bookbuf, header->packet, header->bytes);
368 header = &header_codebooks;
373 buffer = ogg_sync_buffer (state->oy, CHUNKSIZE);
374 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
376 if (bytes == 0 && i < 2) {
377 ret = VCEDIT_ERR_INVAL;
381 ogg_sync_wrote (state->oy, bytes);
384 /* Copy the vendor tag */
385 state->vendor = strdup (state->vc->vendor);
387 /* Headers are done! */
388 state->opened = true;
390 return VCEDIT_ERR_SUCCESS;
393 vcedit_clear_internals (state);
399 vcedit_write (vcedit_state *state)
401 ogg_stream_state streamout;
402 ogg_packet header_main, header_comments, header_codebooks, op;
403 ogg_page ogout, ogin;
404 ogg_int64_t granpos = 0;
406 char *buffer, tmpfile[PATH_MAX];
407 int s, result, bytes, needflush = 0, needout = 0;
411 return VCEDIT_ERR_INVAL;
413 strcpy (tmpfile, state->filename);
414 strcat (tmpfile, ".XXXXXX");
416 s = mkstemp (tmpfile);
418 return VCEDIT_ERR_TMPFILE;
420 out = fdopen (s, "wb");
425 return VCEDIT_ERR_TMPFILE;
428 state->prevW = state->extrapage = state->eosin = 0;
430 header_main.bytes = state->mainlen;
431 header_main.packet = state->mainbuf;
432 header_main.b_o_s = 1;
433 header_main.e_o_s = 0;
434 header_main.granulepos = 0;
436 header_codebooks.bytes = state->booklen;
437 header_codebooks.packet = state->bookbuf;
438 header_codebooks.b_o_s = 0;
439 header_codebooks.e_o_s = 0;
440 header_codebooks.granulepos = 0;
442 ogg_stream_init (&streamout, state->serial);
444 _commentheader_out (state->vc, state->vendor, &header_comments);
446 ogg_stream_packetin (&streamout, &header_main);
447 ogg_stream_packetin (&streamout, &header_comments);
448 ogg_stream_packetin (&streamout, &header_codebooks);
450 while ((result = ogg_stream_flush (&streamout, &ogout))) {
451 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
452 if (tmp != (size_t) ogout.header_len)
455 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
456 if (tmp != (size_t) ogout.body_len)
460 while (_fetch_next_packet (state, &op, &ogin)) {
463 size = _blocksize (state, &op);
467 if (ogg_stream_flush (&streamout, &ogout)) {
468 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
469 if (tmp != (size_t) ogout.header_len)
472 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
473 if (tmp != (size_t) ogout.body_len)
476 } else if (needout) {
477 if (ogg_stream_pageout (&streamout, &ogout)) {
478 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
479 if (tmp != (size_t) ogout.header_len)
482 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
483 if (tmp != (size_t) ogout.body_len)
488 needflush = needout = 0;
490 if (op.granulepos == -1) {
491 op.granulepos = granpos;
492 ogg_stream_packetin (&streamout, &op);
494 /* granulepos is set, validly. Use it, and force a flush to
495 * account for shortened blocks (vcut) when appropriate
497 if (granpos > op.granulepos) {
498 granpos = op.granulepos;
499 ogg_stream_packetin (&streamout, &op);
502 ogg_stream_packetin (&streamout, &op);
510 while (ogg_stream_flush (&streamout, &ogout)) {
511 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
512 if (tmp != (size_t) ogout.header_len)
515 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
516 if (tmp != (size_t) ogout.body_len)
520 if (state->extrapage) {
521 tmp = fwrite (ogin.header, 1, ogin.header_len, out);
522 if (tmp != (size_t) ogin.header_len)
525 tmp = fwrite (ogin.body, 1, ogin.body_len, out);
526 if (tmp != (size_t) ogin.body_len)
530 /* clear it, because not all paths to here do */
533 while (!state->eosin) { /* We reached eos, not eof */
534 /* We copy the rest of the stream (other logical streams)
535 * through, a page at a time.
538 result = ogg_sync_pageout (state->oy, &ogout);
544 /* Don't bother going through the rest, we can just
545 * write the page out now
547 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
548 if (tmp != (size_t) ogout.header_len)
551 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
552 if (tmp != (size_t) ogout.body_len)
557 buffer = ogg_sync_buffer (state->oy, CHUNKSIZE);
558 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
559 ogg_sync_wrote (state->oy, bytes);
570 unlink (state->filename);
571 rename (tmpfile, state->filename);
574 ogg_stream_clear (&streamout);
576 /* We don't ogg_packet_clear() this, because the memory was
577 * allocated in _commentheader_out(), so we mirror that here
579 _ogg_free (header_comments.packet);
581 free (state->mainbuf);
582 free (state->bookbuf);
584 state->mainbuf = state->bookbuf = NULL;
587 return VCEDIT_ERR_INVAL;
589 vcedit_clear_internals (state);
591 return (vcedit_open (state) == VCEDIT_ERR_SUCCESS) ?
592 VCEDIT_ERR_SUCCESS : VCEDIT_ERR_REOPEN;