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)
61 free (state->mainbuf);
62 free (state->bookbuf);
74 vcedit_state_init (vcedit_state *state)
82 vcedit_state_new (const char *filename)
86 state = malloc (sizeof (vcedit_state));
90 memset (state, 0, sizeof (vcedit_state));
92 if (!vcedit_state_init (state)) {
93 vcedit_state_free (state);
97 snprintf (state->filename, sizeof (state->filename),
104 vcedit_comments (vcedit_state *state)
106 return state->opened ? &state->vc : NULL;
110 vcedit_clear_internals (vcedit_state *state)
112 ogg_stream_clear (&state->os);
113 ogg_sync_clear (&state->oy);
115 vorbis_info_clear (&state->vi);
116 vorbis_comment_clear (&state->vc);
118 free (state->vendor);
119 state->vendor = NULL;
121 free (state->mainbuf);
122 state->mainbuf = NULL;
125 free (state->bookbuf);
126 state->bookbuf = NULL;
130 state->opened = false;
134 vcedit_state_ref (vcedit_state *state)
140 vcedit_state_unref (vcedit_state *state)
142 if (--state->refcount)
146 vcedit_clear_internals (state);
148 vcedit_state_free (state);
151 /* Next two functions pulled straight from libvorbis, apart from one change
152 * - we don't want to overwrite the vendor string.
155 _v_writestring (oggpack_buffer *o, char *s, int len)
158 oggpack_write (o, *s++, 8);
163 _commentheader_out (vorbis_comment *vc, char *vendor, ogg_packet *op)
169 oggpack_writeinit (&opb);
172 oggpack_write (&opb, 0x03, 8);
173 _v_writestring (&opb, "vorbis", 6);
176 oggpack_write (&opb, strlen (vendor), 32);
177 _v_writestring (&opb, vendor, strlen (vendor));
180 oggpack_write (&opb, vc->comments, 32);
182 for (i = 0; i < vc->comments; i++) {
183 if (!vc->user_comments[i])
184 oggpack_write (&opb, 0, 32);
186 oggpack_write (&opb, vc->comment_lengths[i], 32);
187 _v_writestring (&opb, vc->user_comments[i],
188 vc->comment_lengths[i]);
192 oggpack_write (&opb, 1, 1);
194 op->packet = _ogg_malloc (oggpack_bytes (&opb));
195 memcpy (op->packet, opb.buffer, oggpack_bytes (&opb));
197 op->bytes = oggpack_bytes (&opb);
202 oggpack_writeclear (&opb);
208 _blocksize (vcedit_state *s, ogg_packet *p)
212 this = vorbis_packet_blocksize (&s->vi, p);
215 ret = (this + s->prevW) / 4;
223 _fetch_next_packet (vcedit_state *s, ogg_packet *p, ogg_page *page)
228 result = ogg_stream_packetout (&s->os, p);
236 while (ogg_sync_pageout (&s->oy, page) <= 0) {
237 buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
238 bytes = fread (buffer, 1, CHUNKSIZE, s->in);
239 ogg_sync_wrote (&s->oy, bytes);
245 if (ogg_page_eos (page))
247 else if (ogg_page_serialno (page) != s->serial) {
253 ogg_stream_pagein (&s->os, page);
255 return _fetch_next_packet (s, p, page);
259 vcedit_open (vcedit_state *state)
266 ogg_packet header_main, header_comments, header_codebooks;
269 state->in = fopen (state->filename, "rb");
271 return VCEDIT_ERR_OPEN;
273 ogg_sync_init (&state->oy);
276 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
277 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
279 ogg_sync_wrote (&state->oy, bytes);
281 if (ogg_sync_pageout (&state->oy, &og) == 1)
284 /* Bail if we don't find data in the first 40 kB */
285 if (chunks++ >= 10) {
286 ogg_sync_clear (&state->oy);
288 return VCEDIT_ERR_INVAL;
292 state->serial = ogg_page_serialno (&og);
294 ogg_stream_init (&state->os, state->serial);
295 vorbis_info_init (&state->vi);
296 vorbis_comment_init (&state->vc);
298 if (ogg_stream_pagein (&state->os, &og) < 0) {
299 ret = VCEDIT_ERR_INVAL;
303 if (ogg_stream_packetout (&state->os, &header_main) != 1) {
304 ret = VCEDIT_ERR_INVAL;
308 if (vorbis_synthesis_headerin (&state->vi, &state->vc, &header_main) < 0) {
309 ret = VCEDIT_ERR_INVAL;
313 state->mainlen = header_main.bytes;
314 state->mainbuf = malloc (state->mainlen);
315 memcpy (state->mainbuf, header_main.packet, header_main.bytes);
318 header = &header_comments;
322 int result = ogg_sync_pageout (&state->oy, &og);
325 break; /* Too little data so far */
328 ogg_stream_pagein (&state->os, &og);
331 result = ogg_stream_packetout (&state->os, header);
337 ret = VCEDIT_ERR_INVAL;
341 vorbis_synthesis_headerin (&state->vi, &state->vc, header);
344 state->booklen = header->bytes;
345 state->bookbuf = malloc (state->booklen);
346 memcpy (state->bookbuf, header->packet, header->bytes);
350 header = &header_codebooks;
355 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
356 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
358 if (bytes == 0 && 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.
520 result = ogg_sync_pageout (&state->oy, &ogout);
526 /* Don't bother going through the rest, we can just
527 * write the page out now
529 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
530 if (tmp != (size_t) ogout.header_len)
533 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
534 if (tmp != (size_t) ogout.body_len)
539 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
540 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
541 ogg_sync_wrote (&state->oy, bytes);
552 unlink (state->filename);
553 rename (tmpfile, state->filename);
556 ogg_stream_clear (&streamout);
558 /* We don't ogg_packet_clear() this, because the memory was
559 * allocated in _commentheader_out(), so we mirror that here
561 _ogg_free (header_comments.packet);
563 free (state->mainbuf);
564 free (state->bookbuf);
566 state->mainbuf = state->bookbuf = NULL;
569 return VCEDIT_ERR_INVAL;
571 vcedit_clear_internals (state);
573 return (vcedit_open (state) == VCEDIT_ERR_SUCCESS) ?
574 VCEDIT_ERR_SUCCESS : VCEDIT_ERR_REOPEN;