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)
82 vcedit_state_new (const char *filename)
87 len = strlen (filename);
89 state = malloc (sizeof (vcedit_state) + len + 1);
93 memset (state, 0, sizeof (vcedit_state));
95 if (!vcedit_state_init (state)) {
96 vcedit_state_free (state);
100 strcpy (state->filename, filename);
106 vcedit_comments (vcedit_state *state)
108 return state->opened ? &state->vc : NULL;
112 vcedit_clear_internals (vcedit_state *state)
114 ogg_stream_clear (&state->os);
115 ogg_sync_clear (&state->oy);
117 vorbis_info_clear (&state->vi);
118 vorbis_comment_clear (&state->vc);
120 free (state->vendor);
121 state->vendor = NULL;
123 free (state->mainbuf);
124 state->mainbuf = NULL;
127 free (state->bookbuf);
128 state->bookbuf = NULL;
132 state->opened = false;
136 vcedit_state_ref (vcedit_state *state)
142 vcedit_state_unref (vcedit_state *state)
144 if (--state->refcount)
148 vcedit_clear_internals (state);
150 vcedit_state_free (state);
153 /* Next two functions pulled straight from libvorbis, apart from one change
154 * - we don't want to overwrite the vendor string.
157 _v_writestring (oggpack_buffer *o, char *s, int len)
160 oggpack_write (o, *s++, 8);
165 _commentheader_out (vorbis_comment *vc, char *vendor, ogg_packet *op)
171 oggpack_writeinit (&opb);
174 oggpack_write (&opb, 0x03, 8);
175 _v_writestring (&opb, "vorbis", 6);
178 oggpack_write (&opb, strlen (vendor), 32);
179 _v_writestring (&opb, vendor, strlen (vendor));
182 oggpack_write (&opb, vc->comments, 32);
184 for (i = 0; i < vc->comments; i++) {
185 if (!vc->user_comments[i])
186 oggpack_write (&opb, 0, 32);
188 oggpack_write (&opb, vc->comment_lengths[i], 32);
189 _v_writestring (&opb, vc->user_comments[i],
190 vc->comment_lengths[i]);
194 oggpack_write (&opb, 1, 1);
196 op->packet = _ogg_malloc (oggpack_bytes (&opb));
197 memcpy (op->packet, opb.buffer, oggpack_bytes (&opb));
199 op->bytes = oggpack_bytes (&opb);
204 oggpack_writeclear (&opb);
210 _blocksize (vcedit_state *s, ogg_packet *p)
214 this = vorbis_packet_blocksize (&s->vi, p);
217 ret = (this + s->prevW) / 4;
225 _fetch_next_packet (vcedit_state *s, ogg_packet *p, ogg_page *page)
230 result = ogg_stream_packetout (&s->os, p);
238 while (ogg_sync_pageout (&s->oy, page) <= 0) {
239 buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
240 bytes = fread (buffer, 1, CHUNKSIZE, s->in);
241 ogg_sync_wrote (&s->oy, bytes);
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 *state)
268 ogg_packet header_main, header_comments, header_codebooks;
271 state->in = fopen (state->filename, "rb");
273 return VCEDIT_ERR_OPEN;
275 ogg_sync_init (&state->oy);
278 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
279 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
281 ogg_sync_wrote (&state->oy, bytes);
283 if (ogg_sync_pageout (&state->oy, &og) == 1)
286 /* Bail if we don't find data in the first 40 kB */
287 if (chunks++ >= 10) {
288 ogg_sync_clear (&state->oy);
290 return VCEDIT_ERR_INVAL;
294 state->serial = ogg_page_serialno (&og);
296 ogg_stream_init (&state->os, state->serial);
297 vorbis_info_init (&state->vi);
298 vorbis_comment_init (&state->vc);
300 if (ogg_stream_pagein (&state->os, &og) < 0) {
301 ret = VCEDIT_ERR_INVAL;
305 if (ogg_stream_packetout (&state->os, &header_main) != 1) {
306 ret = VCEDIT_ERR_INVAL;
310 if (vorbis_synthesis_headerin (&state->vi, &state->vc, &header_main) < 0) {
311 ret = VCEDIT_ERR_INVAL;
315 state->mainlen = header_main.bytes;
316 state->mainbuf = malloc (state->mainlen);
317 memcpy (state->mainbuf, header_main.packet, header_main.bytes);
320 header = &header_comments;
324 int result = ogg_sync_pageout (&state->oy, &og);
327 break; /* Too little data so far */
330 ogg_stream_pagein (&state->os, &og);
333 result = ogg_stream_packetout (&state->os, header);
339 ret = VCEDIT_ERR_INVAL;
343 vorbis_synthesis_headerin (&state->vi, &state->vc, header);
346 state->booklen = header->bytes;
347 state->bookbuf = malloc (state->booklen);
348 memcpy (state->bookbuf, header->packet, header->bytes);
352 header = &header_codebooks;
357 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
358 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
360 if (bytes == 0 && i < 2) {
361 ret = VCEDIT_ERR_INVAL;
365 ogg_sync_wrote (&state->oy, bytes);
368 /* Copy the vendor tag */
369 state->vendor = strdup (state->vc.vendor);
371 /* Headers are done! */
372 state->opened = true;
374 return VCEDIT_ERR_SUCCESS;
377 vcedit_clear_internals (state);
383 vcedit_write (vcedit_state *state)
385 ogg_stream_state streamout;
386 ogg_packet header_main, header_comments, header_codebooks, op;
387 ogg_page ogout, ogin;
388 ogg_int64_t granpos = 0;
390 char *buffer, tmpfile[PATH_MAX];
391 int s, result, bytes, needflush = 0, needout = 0;
395 return VCEDIT_ERR_INVAL;
397 strcpy (tmpfile, state->filename);
398 strcat (tmpfile, ".XXXXXX");
400 s = mkstemp (tmpfile);
402 return VCEDIT_ERR_TMPFILE;
404 out = fdopen (s, "wb");
409 return VCEDIT_ERR_TMPFILE;
412 state->prevW = state->extrapage = state->eosin = 0;
414 header_main.bytes = state->mainlen;
415 header_main.packet = state->mainbuf;
416 header_main.b_o_s = 1;
417 header_main.e_o_s = 0;
418 header_main.granulepos = 0;
420 header_codebooks.bytes = state->booklen;
421 header_codebooks.packet = state->bookbuf;
422 header_codebooks.b_o_s = 0;
423 header_codebooks.e_o_s = 0;
424 header_codebooks.granulepos = 0;
426 ogg_stream_init (&streamout, state->serial);
428 _commentheader_out (&state->vc, state->vendor, &header_comments);
430 ogg_stream_packetin (&streamout, &header_main);
431 ogg_stream_packetin (&streamout, &header_comments);
432 ogg_stream_packetin (&streamout, &header_codebooks);
434 while ((result = ogg_stream_flush (&streamout, &ogout))) {
435 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
436 if (tmp != (size_t) ogout.header_len)
439 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
440 if (tmp != (size_t) ogout.body_len)
444 while (_fetch_next_packet (state, &op, &ogin)) {
447 size = _blocksize (state, &op);
451 if (ogg_stream_flush (&streamout, &ogout)) {
452 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
453 if (tmp != (size_t) ogout.header_len)
456 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
457 if (tmp != (size_t) ogout.body_len)
460 } else if (needout) {
461 if (ogg_stream_pageout (&streamout, &ogout)) {
462 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
463 if (tmp != (size_t) ogout.header_len)
466 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
467 if (tmp != (size_t) ogout.body_len)
472 needflush = needout = 0;
474 if (op.granulepos == -1) {
475 op.granulepos = granpos;
476 ogg_stream_packetin (&streamout, &op);
478 /* granulepos is set, validly. Use it, and force a flush to
479 * account for shortened blocks (vcut) when appropriate
481 if (granpos > op.granulepos) {
482 granpos = op.granulepos;
483 ogg_stream_packetin (&streamout, &op);
486 ogg_stream_packetin (&streamout, &op);
494 while (ogg_stream_flush (&streamout, &ogout)) {
495 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
496 if (tmp != (size_t) ogout.header_len)
499 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
500 if (tmp != (size_t) ogout.body_len)
504 if (state->extrapage) {
505 tmp = fwrite (ogin.header, 1, ogin.header_len, out);
506 if (tmp != (size_t) ogin.header_len)
509 tmp = fwrite (ogin.body, 1, ogin.body_len, out);
510 if (tmp != (size_t) ogin.body_len)
514 /* clear it, because not all paths to here do */
517 while (!state->eosin) { /* We reached eos, not eof */
518 /* We copy the rest of the stream (other logical streams)
519 * through, a page at a time.
522 result = ogg_sync_pageout (&state->oy, &ogout);
528 /* Don't bother going through the rest, we can just
529 * write the page out now
531 tmp = fwrite (ogout.header, 1, ogout.header_len, out);
532 if (tmp != (size_t) ogout.header_len)
535 tmp = fwrite (ogout.body, 1, ogout.body_len, out);
536 if (tmp != (size_t) ogout.body_len)
541 buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
542 bytes = fread (buffer, 1, CHUNKSIZE, state->in);
543 ogg_sync_wrote (&state->oy, bytes);
554 unlink (state->filename);
555 rename (tmpfile, state->filename);
558 ogg_stream_clear (&streamout);
560 /* We don't ogg_packet_clear() this, because the memory was
561 * allocated in _commentheader_out(), so we mirror that here
563 _ogg_free (header_comments.packet);
565 free (state->mainbuf);
566 free (state->bookbuf);
568 state->mainbuf = state->bookbuf = NULL;
571 return VCEDIT_ERR_INVAL;
573 vcedit_clear_internals (state);
575 return (vcedit_open (state) == VCEDIT_ERR_SUCCESS) ?
576 VCEDIT_ERR_SUCCESS : VCEDIT_ERR_REOPEN;