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 bool success = false;
407 int fd, result, bytes, needflush = 0, needout = 0;
410 return VCEDIT_ERR_INVAL;
412 strcpy (tmpfile, s->filename);
413 strcat (tmpfile, ".XXXXXX");
415 fd = mkstemp (tmpfile);
417 return VCEDIT_ERR_TMPFILE;
419 out = fdopen (fd, "wb");
424 return VCEDIT_ERR_TMPFILE;
427 s->prevW = s->extrapage = s->eosin = 0;
429 header_main.bytes = s->mainlen;
430 header_main.packet = s->mainbuf;
431 header_main.b_o_s = 1;
432 header_main.e_o_s = 0;
433 header_main.granulepos = 0;
435 header_codebooks.bytes = s->booklen;
436 header_codebooks.packet = s->bookbuf;
437 header_codebooks.b_o_s = 0;
438 header_codebooks.e_o_s = 0;
439 header_codebooks.granulepos = 0;
441 ogg_stream_init (&streamout, s->serial);
443 _commentheader_out (&s->vc, s->vendor, &header_comments);
445 ogg_stream_packetin (&streamout, &header_main);
446 ogg_stream_packetin (&streamout, &header_comments);
447 ogg_stream_packetin (&streamout, &header_codebooks);
449 while ((result = ogg_stream_flush (&streamout, &ogout))) {
450 if (!write_data (ogout.header, 1, ogout.header_len, out))
453 if (!write_data (ogout.body, 1, ogout.body_len, out))
457 while (_fetch_next_packet (s, &op, &ogin)) {
460 size = _blocksize (s, &op);
464 if (ogg_stream_flush (&streamout, &ogout)) {
465 if (!write_data (ogout.header, 1, ogout.header_len, out))
468 if (!write_data (ogout.body, 1, ogout.body_len, out))
471 } else if (needout) {
472 if (ogg_stream_pageout (&streamout, &ogout)) {
473 if (!write_data (ogout.header, 1, ogout.header_len, out))
476 if (!write_data (ogout.body, 1, ogout.body_len, out))
481 needflush = needout = 0;
483 if (op.granulepos == -1) {
484 op.granulepos = granpos;
485 ogg_stream_packetin (&streamout, &op);
487 /* granulepos is set, validly. Use it, and force a flush to
488 * account for shortened blocks (vcut) when appropriate
490 if (granpos > op.granulepos) {
491 granpos = op.granulepos;
492 ogg_stream_packetin (&streamout, &op);
495 ogg_stream_packetin (&streamout, &op);
503 while (ogg_stream_flush (&streamout, &ogout)) {
504 if (!write_data (ogout.header, 1, ogout.header_len, out))
507 if (!write_data (ogout.body, 1, ogout.body_len, out))
512 if (!write_data (ogin.header, 1, ogin.header_len, out))
515 if (!write_data (ogin.body, 1, ogin.body_len, out))
519 /* clear it, because not all paths to here do */
523 /* We copy the rest of the stream (other logical streams)
524 * through, a page at a time.
526 while ((result = ogg_sync_pageout (&s->oy, &ogout))) {
530 /* Don't bother going through the rest, we can just
531 * write the page out now
533 if (!write_data (ogout.header, 1, ogout.header_len, out))
536 if (!write_data (ogout.body, 1, ogout.body_len, out))
540 buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
541 bytes = fread (buffer, 1, CHUNKSIZE, s->in);
542 ogg_sync_wrote (&s->oy, bytes);
547 s->eosin = !bytes && feof (s->in);
560 unlink (s->filename);
561 rename (tmpfile, s->filename);
564 ogg_stream_clear (&streamout);
566 /* We don't ogg_packet_clear() this, because the memory was
567 * allocated in _commentheader_out(), so we mirror that here
569 _ogg_free (header_comments.packet);
574 s->mainbuf = s->bookbuf = NULL;
577 return VCEDIT_ERR_INVAL;
579 vcedit_clear_internals (s);
581 return (vcedit_open (s) == VCEDIT_ERR_SUCCESS) ?
582 VCEDIT_ERR_SUCCESS : VCEDIT_ERR_REOPEN;