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 *s)
74 vcedit_state_init (vcedit_state *s, const char *filename)
78 strcpy (s->filename, filename);
84 vcedit_state_new (const char *filename)
89 len = strlen (filename);
93 s= malloc (sizeof (vcedit_state) + len + 1);
97 memset (s, 0, sizeof (vcedit_state));
99 if (!vcedit_state_init (s, filename)) {
100 vcedit_state_free (s);
108 vcedit_comments (vcedit_state *s)
110 return s->opened ? &s->vc : NULL;
114 vcedit_clear_internals (vcedit_state *s)
116 ogg_stream_clear (&s->os);
117 ogg_sync_clear (&s->oy);
119 vorbis_info_clear (&s->vi);
120 vorbis_comment_clear (&s->vc);
138 vcedit_state_ref (vcedit_state *s)
144 vcedit_state_unref (vcedit_state *s)
150 vcedit_clear_internals (s);
152 vcedit_state_free (s);
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) || ferror (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 *s)
265 ogg_packet header_main, header_comments, header_codebooks;
268 size_t bytes, total = 0;
271 s->in = fopen (s->filename, "rb");
273 return VCEDIT_ERR_OPEN;
275 ogg_sync_init (&s->oy);
278 /* Bail if we don't find data in the first 40 kB */
279 if (feof (s->in) || ferror (s->in) || total >= (CHUNKSIZE * 10)) {
280 ogg_sync_clear (&s->oy);
282 return VCEDIT_ERR_INVAL;
285 buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
287 bytes = fread (buffer, 1, CHUNKSIZE, s->in);
290 ogg_sync_wrote (&s->oy, bytes);
291 } while (ogg_sync_pageout (&s->oy, &og) != 1);
293 s->serial = ogg_page_serialno (&og);
295 ogg_stream_init (&s->os, s->serial);
296 vorbis_info_init (&s->vi);
297 vorbis_comment_init (&s->vc);
299 if (ogg_stream_pagein (&s->os, &og) < 0) {
300 ret = VCEDIT_ERR_INVAL;
304 if (ogg_stream_packetout (&s->os, &header_main) != 1) {
305 ret = VCEDIT_ERR_INVAL;
309 if (vorbis_synthesis_headerin (&s->vi, &s->vc, &header_main) < 0) {
310 ret = VCEDIT_ERR_INVAL;
314 s->mainlen = header_main.bytes;
315 s->mainbuf = malloc (s->mainlen);
316 memcpy (s->mainbuf, header_main.packet, header_main.bytes);
318 header = &header_comments;
321 if (feof (s->in) || ferror (s->in)) {
322 ret = VCEDIT_ERR_INVAL;
329 result = ogg_sync_pageout (&s->oy, &og);
331 break; /* Too little data so far */
336 ogg_stream_pagein (&s->os, &og);
339 result = ogg_stream_packetout (&s->os, header);
344 ret = VCEDIT_ERR_INVAL;
348 vorbis_synthesis_headerin (&s->vi, &s->vc, header);
351 s->booklen = header->bytes;
352 s->bookbuf = malloc (s->booklen);
353 memcpy (s->bookbuf, header->packet, header->bytes);
356 header = &header_codebooks;
360 buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
362 bytes = fread (buffer, 1, CHUNKSIZE, s->in);
363 ogg_sync_wrote (&s->oy, bytes);
366 /* Copy the vendor tag */
367 s->vendor = strdup (s->vc.vendor);
369 /* Headers are done! */
372 return VCEDIT_ERR_SUCCESS;
375 vcedit_clear_internals (s);
381 write_data (const void *buf, size_t size, size_t nmemb, FILE *stream)
386 w = fwrite (buf, size, nmemb, stream);
387 if (!w && ferror (stream))
398 vcedit_write (vcedit_state *s)
400 ogg_stream_state streamout;
401 ogg_packet header_main, header_comments, header_codebooks, op;
402 ogg_page ogout, ogin;
403 ogg_int64_t granpos = 0;
405 char *buffer, tmpfile[PATH_MAX];
406 int fd, result, bytes, needflush = 0, needout = 0;
409 return VCEDIT_ERR_INVAL;
411 strcpy (tmpfile, s->filename);
412 strcat (tmpfile, ".XXXXXX");
414 fd = mkstemp (tmpfile);
416 return VCEDIT_ERR_TMPFILE;
418 out = fdopen (fd, "wb");
423 return VCEDIT_ERR_TMPFILE;
426 s->prevW = s->extrapage = s->eosin = 0;
428 header_main.bytes = s->mainlen;
429 header_main.packet = s->mainbuf;
430 header_main.b_o_s = 1;
431 header_main.e_o_s = 0;
432 header_main.granulepos = 0;
434 header_codebooks.bytes = s->booklen;
435 header_codebooks.packet = s->bookbuf;
436 header_codebooks.b_o_s = 0;
437 header_codebooks.e_o_s = 0;
438 header_codebooks.granulepos = 0;
440 ogg_stream_init (&streamout, s->serial);
442 _commentheader_out (&s->vc, s->vendor, &header_comments);
444 ogg_stream_packetin (&streamout, &header_main);
445 ogg_stream_packetin (&streamout, &header_comments);
446 ogg_stream_packetin (&streamout, &header_codebooks);
448 while ((result = ogg_stream_flush (&streamout, &ogout))) {
449 if (!write_data (ogout.header, 1, ogout.header_len, out))
452 if (!write_data (ogout.body, 1, ogout.body_len, out))
456 while (_fetch_next_packet (s, &op, &ogin)) {
459 size = _blocksize (s, &op);
463 if (ogg_stream_flush (&streamout, &ogout)) {
464 if (!write_data (ogout.header, 1, ogout.header_len, out))
467 if (!write_data (ogout.body, 1, ogout.body_len, out))
470 } else if (needout) {
471 if (ogg_stream_pageout (&streamout, &ogout)) {
472 if (!write_data (ogout.header, 1, ogout.header_len, out))
475 if (!write_data (ogout.body, 1, ogout.body_len, out))
480 needflush = needout = 0;
482 if (op.granulepos == -1) {
483 op.granulepos = granpos;
484 ogg_stream_packetin (&streamout, &op);
486 /* granulepos is set, validly. Use it, and force a flush to
487 * account for shortened blocks (vcut) when appropriate
489 if (granpos > op.granulepos) {
490 granpos = op.granulepos;
491 ogg_stream_packetin (&streamout, &op);
494 ogg_stream_packetin (&streamout, &op);
502 while (ogg_stream_flush (&streamout, &ogout)) {
503 if (!write_data (ogout.header, 1, ogout.header_len, out))
506 if (!write_data (ogout.body, 1, ogout.body_len, out))
511 if (!write_data (ogin.header, 1, ogin.header_len, out))
514 if (!write_data (ogin.body, 1, ogin.body_len, out))
518 /* clear it, because not all paths to here do */
522 /* We copy the rest of the stream (other logical streams)
523 * through, a page at a time.
525 while ((result = ogg_sync_pageout (&s->oy, &ogout))) {
529 /* Don't bother going through the rest, we can just
530 * write the page out now
532 if (!write_data (ogout.header, 1, ogout.header_len, out))
535 if (!write_data (ogout.body, 1, ogout.body_len, out))
539 buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
540 bytes = fread (buffer, 1, CHUNKSIZE, s->in);
541 ogg_sync_wrote (&s->oy, bytes);
546 s->eosin = !bytes && feof (s->in);
552 unlink (s->filename);
553 rename (tmpfile, s->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);
566 s->mainbuf = s->bookbuf = NULL;
569 return VCEDIT_ERR_INVAL;
571 vcedit_clear_internals (s);
573 return (vcedit_open (s) == VCEDIT_ERR_SUCCESS) ?
574 VCEDIT_ERR_SUCCESS : VCEDIT_ERR_REOPEN;