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);
167 _commentheader_out (vorbis_comment *vc, char *vendor, ogg_packet *op)
173 oggpack_writeinit (&opb);
176 oggpack_write (&opb, 0x03, 8);
177 _v_writestring (&opb, "vorbis", 6);
180 oggpack_write (&opb, strlen (vendor), 32);
181 _v_writestring (&opb, vendor, strlen (vendor));
184 oggpack_write (&opb, vc->comments, 32);
186 for (i = 0; i < vc->comments; i++) {
187 if (!vc->user_comments[i])
188 oggpack_write (&opb, 0, 32);
190 oggpack_write (&opb, vc->comment_lengths[i], 32);
191 _v_writestring (&opb, vc->user_comments[i],
192 vc->comment_lengths[i]);
196 oggpack_write (&opb, 1, 1);
198 op->packet = _ogg_malloc (oggpack_bytes (&opb));
199 memcpy (op->packet, opb.buffer, oggpack_bytes (&opb));
201 op->bytes = oggpack_bytes (&opb);
206 oggpack_writeclear (&opb);
212 _blocksize (vcedit_state *s, ogg_packet *p)
216 this = vorbis_packet_blocksize (&s->vi, p);
219 ret = (this + s->prevW) / 4;
227 _fetch_next_packet (vcedit_state *s, ogg_packet *p, ogg_page *page)
232 result = ogg_stream_packetout (&s->os, p);
240 while (ogg_sync_pageout (&s->oy, page) <= 0) {
241 buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
242 bytes = fread (buffer, 1, CHUNKSIZE, s->in);
243 ogg_sync_wrote (&s->oy, bytes);
249 if (ogg_page_eos (page))
251 else if (ogg_page_serialno (page) != s->serial) {
257 ogg_stream_pagein (&s->os, page);
259 return _fetch_next_packet (s, p, page);
263 vcedit_open (vcedit_state *state)
270 ogg_packet header_main, header_comments, header_codebooks;
273 state->in = fopen (state->filename, "rb");
275 return VCEDIT_ERR_OPEN;
277 ogg_sync_init (&state->oy);
280 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
281 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
283 ogg_sync_wrote (&state->oy, bytes);
285 if (ogg_sync_pageout (&state->oy, &og) == 1)
288 /* Bail if we don't find data in the first 40 kB */
289 if (chunks++ >= 10) {
290 ogg_sync_clear (&state->oy);
292 return VCEDIT_ERR_INVAL;
296 state->serial = ogg_page_serialno (&og);
298 ogg_stream_init (&state->os, state->serial);
299 vorbis_info_init (&state->vi);
300 vorbis_comment_init (&state->vc);
302 if (ogg_stream_pagein (&state->os, &og) < 0) {
303 ret = VCEDIT_ERR_INVAL;
307 if (ogg_stream_packetout (&state->os, &header_main) != 1) {
308 ret = VCEDIT_ERR_INVAL;
312 if (vorbis_synthesis_headerin (&state->vi, &state->vc, &header_main) < 0) {
313 ret = VCEDIT_ERR_INVAL;
317 state->mainlen = header_main.bytes;
318 state->mainbuf = malloc (state->mainlen);
319 memcpy (state->mainbuf, header_main.packet, header_main.bytes);
322 header = &header_comments;
326 int result = ogg_sync_pageout (&state->oy, &og);
329 break; /* Too little data so far */
332 ogg_stream_pagein (&state->os, &og);
335 result = ogg_stream_packetout (&state->os, header);
341 ret = VCEDIT_ERR_INVAL;
345 vorbis_synthesis_headerin (&state->vi, &state->vc, header);
348 state->booklen = header->bytes;
349 state->bookbuf = malloc (state->booklen);
350 memcpy (state->bookbuf, header->packet, header->bytes);
354 header = &header_codebooks;
359 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
360 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
362 if (bytes == 0 && i < 2) {
363 ret = VCEDIT_ERR_INVAL;
367 ogg_sync_wrote (&state->oy, bytes);
370 /* Copy the vendor tag */
371 state->vendor = strdup (state->vc.vendor);
373 /* Headers are done! */
374 state->opened = true;
376 return VCEDIT_ERR_SUCCESS;
379 vcedit_clear_internals (state);
385 vcedit_write (vcedit_state *state)
387 ogg_stream_state streamout;
388 ogg_packet header_main, header_comments, header_codebooks, op;
389 ogg_page ogout, ogin;
390 ogg_int64_t granpos = 0;
392 char *buffer, tmpfile[PATH_MAX];
393 int s, result, bytes, needflush = 0, needout = 0;
397 return VCEDIT_ERR_INVAL;
399 strcpy (tmpfile, state->filename);
400 strcat (tmpfile, ".XXXXXX");
402 s = mkstemp (tmpfile);
404 return VCEDIT_ERR_TMPFILE;
406 out = fdopen (s, "wb");
411 return VCEDIT_ERR_TMPFILE;
414 state->prevW = state->extrapage = state->eosin = 0;
416 header_main.bytes = state->mainlen;
417 header_main.packet = state->mainbuf;
418 header_main.b_o_s = 1;
419 header_main.e_o_s = 0;
420 header_main.granulepos = 0;
422 header_codebooks.bytes = state->booklen;
423 header_codebooks.packet = state->bookbuf;
424 header_codebooks.b_o_s = 0;
425 header_codebooks.e_o_s = 0;
426 header_codebooks.granulepos = 0;
428 ogg_stream_init (&streamout, state->serial);
430 _commentheader_out (&state->vc, state->vendor, &header_comments);
432 ogg_stream_packetin (&streamout, &header_main);
433 ogg_stream_packetin (&streamout, &header_comments);
434 ogg_stream_packetin (&streamout, &header_codebooks);
436 while ((result = ogg_stream_flush (&streamout, &ogout))) {
437 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
438 if (tmp != (size_t) ogout.header_len)
441 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
442 if (tmp != (size_t) ogout.body_len)
446 while (_fetch_next_packet (state, &op, &ogin)) {
449 size = _blocksize (state, &op);
453 if (ogg_stream_flush (&streamout, &ogout)) {
454 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
455 if (tmp != (size_t) ogout.header_len)
458 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
459 if (tmp != (size_t) ogout.body_len)
462 } else if (needout) {
463 if (ogg_stream_pageout (&streamout, &ogout)) {
464 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
465 if (tmp != (size_t) ogout.header_len)
468 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
469 if (tmp != (size_t) ogout.body_len)
474 needflush = needout = 0;
476 if (op.granulepos == -1) {
477 op.granulepos = granpos;
478 ogg_stream_packetin (&streamout, &op);
480 /* granulepos is set, validly. Use it, and force a flush to
481 * account for shortened blocks (vcut) when appropriate
483 if (granpos > op.granulepos) {
484 granpos = op.granulepos;
485 ogg_stream_packetin (&streamout, &op);
488 ogg_stream_packetin (&streamout, &op);
496 while (ogg_stream_flush (&streamout, &ogout)) {
497 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
498 if (tmp != (size_t) ogout.header_len)
501 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
502 if (tmp != (size_t) ogout.body_len)
506 if (state->extrapage) {
507 tmp = fwrite (ogin.header, 1, ogin.header_len, out);
508 if (tmp != (size_t) ogin.header_len)
511 tmp = fwrite (ogin.body, 1, ogin.body_len, out);
512 if (tmp != (size_t) ogin.body_len)
516 /* clear it, because not all paths to here do */
519 while (!state->eosin) { /* We reached eos, not eof */
520 /* We copy the rest of the stream (other logical streams)
521 * through, a page at a time.
524 result = ogg_sync_pageout (&state->oy, &ogout);
530 /* Don't bother going through the rest, we can just
531 * write the page out now
533 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
534 if (tmp != (size_t) ogout.header_len)
537 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
538 if (tmp != (size_t) ogout.body_len)
543 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
544 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
545 ogg_sync_wrote (&state->oy, bytes);
556 unlink (state->filename);
557 rename (tmpfile, state->filename);
560 ogg_stream_clear (&streamout);
562 /* We don't ogg_packet_clear() this, because the memory was
563 * allocated in _commentheader_out(), so we mirror that here
565 _ogg_free (header_comments.packet);
567 free (state->mainbuf);
568 free (state->bookbuf);
570 state->mainbuf = state->bookbuf = NULL;
573 return VCEDIT_ERR_INVAL;
575 vcedit_clear_internals (state);
577 return (vcedit_open (state) == VCEDIT_ERR_SUCCESS) ?
578 VCEDIT_ERR_SUCCESS : VCEDIT_ERR_REOPEN;