projects
/
ruby-vorbistagger.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Slightly reworked the final write loop.
[ruby-vorbistagger.git]
/
ext
/
vcedit.c
diff --git
a/ext/vcedit.c
b/ext/vcedit.c
index 1aee14c25ed1fcf4cd0b0ba2fb2d53edd903b3fa..75bf80db99aac4b3205172cb5c14b520b4d9193a 100644
(file)
--- a/
ext/vcedit.c
+++ b/
ext/vcedit.c
@@
-34,13
+34,11
@@
struct vcedit_state_St {
int refcount;
struct vcedit_state_St {
int refcount;
- ogg_sync_state
*
oy;
- ogg_stream_state
*
os;
+ ogg_sync_state oy;
+ ogg_stream_state os;
- vorbis_comment *vc;
- vorbis_info *vi;
-
- char filename[PATH_MAX];
+ vorbis_comment vc;
+ vorbis_info vi;
FILE *in;
bool opened;
FILE *in;
bool opened;
@@
-53,15
+51,13
@@
struct vcedit_state_St {
int prevW;
int extrapage;
int eosin;
int prevW;
int extrapage;
int eosin;
+
+ char filename[0];
};
static void
vcedit_state_free (vcedit_state *state)
{
};
static void
vcedit_state_free (vcedit_state *state)
{
- free (state->oy);
- free (state->os);
- free (state->vc);
- free (state->vi);
free (state->mainbuf);
free (state->bookbuf);
free (state->vendor);
free (state->mainbuf);
free (state->bookbuf);
free (state->vendor);
@@
-75,25
+71,11
@@
vcedit_state_free (vcedit_state *state)
}
static bool
}
static bool
-vcedit_state_init (vcedit_state *state)
+vcedit_state_init (vcedit_state *state
, const char *filename
)
{
state->refcount = 1;
{
state->refcount = 1;
- state->oy = malloc (sizeof (ogg_sync_state));
- if (!state->oy)
- return false;
-
- state->os = malloc (sizeof (ogg_stream_state));
- if (!state->os)
- return false;
-
- state->vc = malloc (sizeof (vorbis_comment));
- if (!state->vc)
- return false;
-
- state->vi = malloc (sizeof (vorbis_info));
- if (!state->vi)
- return false;
+ strcpy (state->filename, filename);
return true;
}
return true;
}
@@
-102,38
+84,40
@@
vcedit_state *
vcedit_state_new (const char *filename)
{
vcedit_state *state;
vcedit_state_new (const char *filename)
{
vcedit_state *state;
+ size_t len;
- state = malloc (sizeof (vcedit_state));
+ len = strlen (filename);
+ if (len > PATH_MAX)
+ return NULL;
+
+ state = malloc (sizeof (vcedit_state) + len + 1);
if (!state)
return NULL;
memset (state, 0, sizeof (vcedit_state));
if (!state)
return NULL;
memset (state, 0, sizeof (vcedit_state));
- if (!vcedit_state_init (state)) {
+ if (!vcedit_state_init (state
, filename
)) {
vcedit_state_free (state);
return NULL;
}
vcedit_state_free (state);
return NULL;
}
- snprintf (state->filename, sizeof (state->filename),
- "%s", filename);
-
return state;
}
vorbis_comment *
vcedit_comments (vcedit_state *state)
{
return state;
}
vorbis_comment *
vcedit_comments (vcedit_state *state)
{
- return state->opened ? state->vc : NULL;
+ return state->opened ?
&
state->vc : NULL;
}
static void
vcedit_clear_internals (vcedit_state *state)
{
}
static void
vcedit_clear_internals (vcedit_state *state)
{
- ogg_stream_clear (state->os);
- ogg_sync_clear (state->oy);
+ ogg_stream_clear (
&
state->os);
+ ogg_sync_clear (
&
state->oy);
- vorbis_info_clear (state->vi);
- vorbis_comment_clear (state->vc);
+ vorbis_info_clear (
&
state->vi);
+ vorbis_comment_clear (
&
state->vc);
free (state->vendor);
state->vendor = NULL;
free (state->vendor);
state->vendor = NULL;
@@
-174,9
+158,8
@@
vcedit_state_unref (vcedit_state *state)
static void
_v_writestring (oggpack_buffer *o, char *s, int len)
{
static void
_v_writestring (oggpack_buffer *o, char *s, int len)
{
- while (len--)
{
+ while (len--)
oggpack_write (o, *s++, 8);
oggpack_write (o, *s++, 8);
- }
}
static int
}
static int
@@
-229,7
+212,7
@@
_blocksize (vcedit_state *s, ogg_packet *p)
{
int this, ret = 0;
{
int this, ret = 0;
- this = vorbis_packet_blocksize (s->vi, p);
+ this = vorbis_packet_blocksize (
&
s->vi, p);
if (s->prevW)
ret = (this + s->prevW) / 4;
if (s->prevW)
ret = (this + s->prevW) / 4;
@@
-245,20
+228,19
@@
_fetch_next_packet (vcedit_state *s, ogg_packet *p, ogg_page *page)
char *buffer;
int result, bytes;
char *buffer;
int result, bytes;
- result = ogg_stream_packetout (s->os, p);
-
- if (result > 0)
+ result = ogg_stream_packetout (&s->os, p);
+ if (result == 1)
return 1;
if (s->eosin)
return 0;
return 1;
if (s->eosin)
return 0;
- while (ogg_sync_pageout (
s->oy, page) <= 0
) {
- buffer = ogg_sync_buffer (s->oy, CHUNKSIZE);
+ while (ogg_sync_pageout (
&s->oy, page) != 1
) {
+ buffer = ogg_sync_buffer (
&
s->oy, CHUNKSIZE);
bytes = fread (buffer, 1, CHUNKSIZE, s->in);
bytes = fread (buffer, 1, CHUNKSIZE, s->in);
- ogg_sync_wrote (s->oy, bytes);
+ ogg_sync_wrote (
&
s->oy, bytes);
- if (!bytes)
+ if (!bytes
&& feof (s->in)
)
return 0;
}
return 0;
}
@@
-270,7
+252,7
@@
_fetch_next_packet (vcedit_state *s, ogg_packet *p, ogg_page *page)
return 0;
}
return 0;
}
- ogg_stream_pagein (s->os, page);
+ ogg_stream_pagein (
&
s->os, page);
return _fetch_next_packet (s, p, page);
}
return _fetch_next_packet (s, p, page);
}
@@
-279,53
+261,52
@@
vcedit_error
vcedit_open (vcedit_state *state)
{
vcedit_error ret;
vcedit_open (vcedit_state *state)
{
vcedit_error ret;
- char *buffer;
- int bytes, i;
- int chunks = 0;
ogg_packet *header;
ogg_packet header_main, header_comments, header_codebooks;
ogg_page og;
ogg_packet *header;
ogg_packet header_main, header_comments, header_codebooks;
ogg_page og;
+ char *buffer;
+ size_t bytes, total = 0;
+ int i = 0;
state->in = fopen (state->filename, "rb");
if (!state->in)
return VCEDIT_ERR_OPEN;
state->in = fopen (state->filename, "rb");
if (!state->in)
return VCEDIT_ERR_OPEN;
- ogg_sync_init (state->oy);
-
- while (1) {
- buffer = ogg_sync_buffer (state->oy, CHUNKSIZE);
- bytes = fread (buffer, 1, CHUNKSIZE, state->in);
-
- ogg_sync_wrote (state->oy, bytes);
-
- if (ogg_sync_pageout (state->oy, &og) == 1)
- break;
+ ogg_sync_init (&state->oy);
+ do {
/* Bail if we don't find data in the first 40 kB */
/* Bail if we don't find data in the first 40 kB */
- if (
chunks++ >= 10
) {
- ogg_sync_clear (state->oy);
+ if (
feof (state->in) || total >= (CHUNKSIZE * 10)
) {
+ ogg_sync_clear (
&
state->oy);
return VCEDIT_ERR_INVAL;
}
return VCEDIT_ERR_INVAL;
}
- }
+
+ buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
+
+ bytes = fread (buffer, 1, CHUNKSIZE, state->in);
+ total += bytes;
+
+ ogg_sync_wrote (&state->oy, bytes);
+ } while (ogg_sync_pageout (&state->oy, &og) != 1);
state->serial = ogg_page_serialno (&og);
state->serial = ogg_page_serialno (&og);
- ogg_stream_init (state->os, state->serial);
- vorbis_info_init (state->vi);
- vorbis_comment_init (state->vc);
+ ogg_stream_init (
&
state->os, state->serial);
+ vorbis_info_init (
&
state->vi);
+ vorbis_comment_init (
&
state->vc);
- if (ogg_stream_pagein (state->os, &og) < 0) {
+ if (ogg_stream_pagein (
&
state->os, &og) < 0) {
ret = VCEDIT_ERR_INVAL;
goto err;
}
ret = VCEDIT_ERR_INVAL;
goto err;
}
- if (ogg_stream_packetout (state->os, &header_main) != 1) {
+ if (ogg_stream_packetout (
&
state->os, &header_main) != 1) {
ret = VCEDIT_ERR_INVAL;
goto err;
}
ret = VCEDIT_ERR_INVAL;
goto err;
}
- if (vorbis_synthesis_headerin (
state->vi,
state->vc, &header_main) < 0) {
+ if (vorbis_synthesis_headerin (
&state->vi, &
state->vc, &header_main) < 0) {
ret = VCEDIT_ERR_INVAL;
goto err;
}
ret = VCEDIT_ERR_INVAL;
goto err;
}
@@
-334,57
+315,56
@@
vcedit_open (vcedit_state *state)
state->mainbuf = malloc (state->mainlen);
memcpy (state->mainbuf, header_main.packet, header_main.bytes);
state->mainbuf = malloc (state->mainlen);
memcpy (state->mainbuf, header_main.packet, header_main.bytes);
- i = 0;
header = &header_comments;
while (i < 2) {
while (i < 2) {
header = &header_comments;
while (i < 2) {
while (i < 2) {
- int result
= ogg_sync_pageout (state->oy, &og)
;
+ int result;
+ result = ogg_sync_pageout (&state->oy, &og);
if (!result)
break; /* Too little data so far */
if (!result)
break; /* Too little data so far */
- if (result
== 1) {
-
ogg_stream_pagein (state->os, &og)
;
+ if (result
!= 1)
+
continue
;
- while (i < 2) {
- result = ogg_stream_packetout (state->os, header);
+ ogg_stream_pagein (&state->os, &og);
- if (!result)
- break;
+ while (i < 2) {
+ result = ogg_stream_packetout (&state->os, header);
+ if (!result)
+ break;
- if (result == -1) {
- ret = VCEDIT_ERR_INVAL;
- goto err;
- }
-
- vorbis_synthesis_headerin (state->vi, state->vc, header);
+ if (result != 1) {
+ ret = VCEDIT_ERR_INVAL;
+ goto err;
+ }
- if (i == 1) {
- state->booklen = header->bytes;
- state->bookbuf = malloc (state->booklen);
- memcpy (state->bookbuf, header->packet, header->bytes);
- }
+ vorbis_synthesis_headerin (&state->vi, &state->vc, header);
- i++;
- header = &header_codebooks;
+ if (i++ == 1) {
+ state->booklen = header->bytes;
+ state->bookbuf = malloc (state->booklen);
+ memcpy (state->bookbuf, header->packet, header->bytes);
}
}
+
+ header = &header_codebooks;
}
}
}
}
- buffer = ogg_sync_buffer (state->oy, CHUNKSIZE);
+ buffer = ogg_sync_buffer (
&
state->oy, CHUNKSIZE);
bytes = fread (buffer, 1, CHUNKSIZE, state->in);
bytes = fread (buffer, 1, CHUNKSIZE, state->in);
- if (
bytes == 0
&& i < 2) {
+ if (
!bytes && feof (state->in)
&& i < 2) {
ret = VCEDIT_ERR_INVAL;
goto err;
}
ret = VCEDIT_ERR_INVAL;
goto err;
}
- ogg_sync_wrote (state->oy, bytes);
+ ogg_sync_wrote (
&
state->oy, bytes);
}
/* Copy the vendor tag */
}
/* Copy the vendor tag */
- state->vendor = strdup (state->vc
->
vendor);
+ state->vendor = strdup (state->vc
.
vendor);
/* Headers are done! */
state->opened = true;
/* Headers are done! */
state->opened = true;
@@
-443,7
+423,7
@@
vcedit_write (vcedit_state *state)
ogg_stream_init (&streamout, state->serial);
ogg_stream_init (&streamout, state->serial);
- _commentheader_out (state->vc, state->vendor, &header_comments);
+ _commentheader_out (
&
state->vc, state->vendor, &header_comments);
ogg_stream_packetin (&streamout, &header_main);
ogg_stream_packetin (&streamout, &header_comments);
ogg_stream_packetin (&streamout, &header_main);
ogg_stream_packetin (&streamout, &header_comments);
@@
-532,39
+512,32
@@
vcedit_write (vcedit_state *state)
/* clear it, because not all paths to here do */
state->eosin = 0;
/* clear it, because not all paths to here do */
state->eosin = 0;
- while (!state->eosin) { /* We reached eos, not eof */
+ do {
/* We copy the rest of the stream (other logical streams)
* through, a page at a time.
*/
/* We copy the rest of the stream (other logical streams)
* through, a page at a time.
*/
- while (1) {
- result = ogg_sync_pageout (state->oy, &ogout);
-
- if (!result)
- break;
+ while ((result = ogg_sync_pageout (&state->oy, &ogout))) {
+ if (result != 1)
+ continue;
- if (result >= 0) {
- /* Don't bother going through the rest, we can just
- * write the page out now
- */
- tmp = fwrite (ogout.header, 1, ogout.header_len, out);
- if (tmp != (size_t) ogout.header_len)
- goto cleanup;
+ /* Don't bother going through the rest, we can just
+ * write the page out now
+ */
+ tmp = fwrite (ogout.header, 1, ogout.header_len, out);
+ if (tmp != (size_t) ogout.header_len)
+ goto cleanup;
- tmp = fwrite (ogout.body, 1, ogout.body_len, out);
- if (tmp != (size_t) ogout.body_len)
- goto cleanup;
- }
+ tmp = fwrite (ogout.body, 1, ogout.body_len, out);
+ if (tmp != (size_t) ogout.body_len)
+ goto cleanup;
}
}
- buffer = ogg_sync_buffer (state->oy, CHUNKSIZE);
+ buffer = ogg_sync_buffer (
&
state->oy, CHUNKSIZE);
bytes = fread (buffer, 1, CHUNKSIZE, state->in);
bytes = fread (buffer, 1, CHUNKSIZE, state->in);
- ogg_sync_wrote (state->oy, bytes);
+ ogg_sync_wrote (
&
state->oy, bytes);
- if (!bytes) {
- state->eosin = 1;
- break;
- }
- }
+ state->eosin = !bytes && feof (state->in);
+ } while (!state->eosin);
fclose (out);
fclose (state->in);
fclose (out);
fclose (state->in);